Powershell:根据创建日期将文件移动到文件夹 [英] Powershell: Move files to folder based on Date Created

查看:65
本文介绍了Powershell:根据创建日期将文件移动到文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是编码员,但我仍然尝试调整此处找到的 PS 脚本,但仍然无法获得我想要的行为.对我来说最困难的部分是 2 位数的 Day 要求 (dd).经过几次菜鸟尝试后,我需要一些帮助.

I'm not a coder but I've still attempted tweaking PS scripts found here and still can't get the behavior I desire. The tough part for me has been the 2 digit Day requirement (dd). After several noob attempts I would like some help.

我有一个包含数百个 JPG 文件的文件夹.我根据拍摄日期手动将这些 JPG 分类到文件夹中.文件夹名称示例为 2015.02.04、2016.10.31、2016.12.01.

I have a folder that contains hundreds of JPG's. I manually sort these JPG's into folders based on the date taken. Folder name examples are 2015.02.04, 2016.10.31, 2016.12.01.

1) 我想要一个脚本来扫描我的 JPG 文件夹.2)对于每个文件,扫描日期3) 如果文件是在 2016 年 6 月 1 日创建的,那么它将被移动到 .\2016.06.01

1) I would like a script to scan my JPG folder. 2) For each file, scan the date 3) If the file was created June 1st, 2016 then it will be moved to .\2016.06.01

帮兄弟帮忙?

$Filepath = ""
$file = ""
$date  = ""
$month   = ""
$year    = ""
$MonthPath   = ""

$FilePath = Read-Host "Place the directory which contains the files."

Write-Warning "Note: This action can take several minutes, depending on the amount of files in $FilePath."

get-childitem $FilePath | % {

  $file = $_.FullName 
    $date = Get-Date ($_.LastWriteTime)

  $month = $date.month
  $year = $date.year
  $day = $date.day
    $MonthPath = "$FilePath\$year.$month.$day"
    Write-Verbose "month = $month"
    Write-Verbose "Date = $date"
    Write-Verbose "year = $year"
    Write-Verbose "FilePath = $FilePath" 
    Write-Verbose "Filename = $file"
    Write-Verbose "MonthPath = $MonthPath"

    if(!(Test-Path -Path "$MonthPath" )){
        Write-Verbose "Creating log location $MonthPath."
        #Write-Host -backgroundcolor green -ForegroundColor black "Creating log location $MonthPath."
        Write-Verbose "MonthPath inside path test = $MonthPath"
        New-Item -ItemType directory -Path $MonthPath | Out-null
    }
    ELSE {
        #Write-Host -backgroundcolor green -ForegroundColor black "Log location exists already exist $MonthPath"
        Write-Verbose "Log location exists already exist $MonthPath" 
        }
    move-item "$file" "$MonthPath" | Out-null
}

Write-Warning "All files are sorted now based upon year and month."

推荐答案

[DateTime]$start_time="2016-6-1 00:00:00"
[DateTime]$end_time="2016-6-1 23:59:59"
$des_folder = "C:\test\2016.06.1"

Get-ChildItem c:\test\*.jpg -Recurse | foreach {if($_.lastwritetime -ge $start_time -and $_.lastwritetime -le $end_time) { move-item $_.fullname $des_folder }}

请确保没有名称冲突.您可以将c:\test*.jpg"中的c:\test"更改为要扫描的路径.还有变量$des_folder"的值到你想要存储匹配图片的目标文件夹.

Please ensure there is no name conflict . You may change"c:\test" in "c:\test*.jpg" to the path you want to scan . Also the value of variable "$des_folder" to the destination folder you want to store the matched pictures .

Get-ChildItem c:\test\test2\*.jpg -Recurse | foreach { 
$x = $_.LastWriteTime.ToShortDateString()
$new_folder_name = Get-Date $x -Format yyyy.MM.dd
$des_path = "c:\test\test2\$new_folder_name"

if (test-path $des_path){ 
    move-item $_.fullname $des_path 
    } else {
    new-item -ItemType directory -Path $des_path
    move-item $_.fullname $des_path 
    }
}

这篇关于Powershell:根据创建日期将文件移动到文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆