Powershell:根据创建日期将文件移动到文件夹

Sab*_*Sab 4 powershell

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

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

1) 我想要一个脚本来扫描我的 JPG 文件夹。2) 对于每个文件,扫描日期 3) 如果文件是在 2016 年 6 月 1 日创建的,那么它将被移动到 .\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."
Run Code Online (Sandbox Code Playgroud)

Elt*_*SFT 6

[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 }}
Run Code Online (Sandbox Code Playgroud)

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

编辑:

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 
    }
}
Run Code Online (Sandbox Code Playgroud)