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

Powershell:根据创建日期将文件移动到文件夹,powershell,Powershell,我不是一个程序员,但我仍然试图调整这里找到的PS脚本,仍然不能得到我想要的行为。对我来说,最困难的部分是2位数的日需求(dd)。经过几次尝试后,我想要一些帮助 我有一个包含数百个JPG的文件夹。我根据拍摄日期手动将这些JPG分类到文件夹中。文件夹名称示例为2015.02.042016.10.312016.12.01 1) 我想要一个脚本来扫描我的JPG文件夹。 2) 对于每个文件,扫描日期 3) 如果文件是在2016年6月1日创建的,那么它将被移动到。\2016.06.01 帮助一个兄弟 $Fi

我不是一个程序员,但我仍然试图调整这里找到的PS脚本,仍然不能得到我想要的行为。对我来说,最困难的部分是2位数的日需求(dd)。经过几次尝试后,我想要一些帮助

我有一个包含数百个JPG的文件夹。我根据拍摄日期手动将这些JPG分类到文件夹中。文件夹名称示例为2015.02.042016.10.312016.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."
请确保没有名称冲突。 您可以将“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 
    }
}

扩展更多内容以覆盖重复项

$jpg_files = Get-ChildItem "F:\*.jpg" -Recurse
foreach ($jpg in $jpg_files){

$x = $jpg.LastWriteTime.ToShortDateString()
$new_folder = Get-Date $x -Format yyyy-MM-dd
$des_path = "F:\Photos\$($new_folder)"

if (Test-Path $des_path){
    if (Test-Path "$($des_path)\$($jpg.Name)"){
        $index = 1
        do {
            $new_name = $des_path + "\" + $jpg.BaseName + " ($($index))" + $jpg.Extension
            $index++
        } While(Test-Path $new_name)

        move-item $jpg.fullname -destination $new_name
    }else {
        move-item $jpg.fullname $des_path
    }
}
else {
    new-item -ItemType directory -Path $des_path
    move-item $jpg.fullname $des_path 
}

我最终创建了一个脚本,完全符合您的要求。您可以,它将具有最新版本的代码

以下是当前的实现,为简洁起见进行了编辑,删除了不必要的功能,并根据问题的需要进行了调整:

[string] $SourceDirectoryPath = 'C:\FilesToMove'
[string] $TargetDirectoryPath = 'C:\SortedFiles'

[System.Collections.ArrayList] $filesToMove = Get-ChildItem -Path $SourceDirectoryPath -File -Force -Recurse

$filesToMove | ForEach-Object {
    [System.IO.FileInfo] $file = $_

    [DateTime] $fileDate = $file.LastWriteTime
    [string] $dateDirectoryName = $fileDate.ToString('yyyy.MM.dd')
    [string] $dateDirectoryPath = Join-Path -Path $TargetDirectoryPath -ChildPath $dateDirectoryName

    if (!(Test-Path -Path $dateDirectoryPath -PathType Container))
    {
        Write-Verbose "Creating directory '$dateDirectoryPath'."
        New-Item -Path $dateDirectoryPath-ItemType Directory -Force > $null
    }

    [string] $filePath = $file.FullName
    Write-Information "Moving file '$filePath' into directory '$dateDirectoryPath'."
    Move-Item -Path $filePath -Destination $dateDirectoryPath
}

请注意,它会在迭代文件路径之前将其复制到数组中。这对于将文件复制到其当前目录的子目录的情况非常重要,否则
Get ChildItem
可能会扫描文件两次,并对其刚刚移动的文件进行迭代。

请共享您正在使用的代码。此代码几乎达到了我想要的效果。当前,如果找到2016年6月1日的日期戳,则将文件移动到。\2016.06.1。我想把它移到。\2016.06.01。谢谢保罗,但不幸的是,这并不是我想要达到的目标。您的脚本将搜索特定日期并仅移动那些目标文件。我正在尝试扫描每个文件并根据日期移动它们。搞定了!谢谢你,先生:)虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高你的文章质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
[string] $SourceDirectoryPath = 'C:\FilesToMove'
[string] $TargetDirectoryPath = 'C:\SortedFiles'

[System.Collections.ArrayList] $filesToMove = Get-ChildItem -Path $SourceDirectoryPath -File -Force -Recurse

$filesToMove | ForEach-Object {
    [System.IO.FileInfo] $file = $_

    [DateTime] $fileDate = $file.LastWriteTime
    [string] $dateDirectoryName = $fileDate.ToString('yyyy.MM.dd')
    [string] $dateDirectoryPath = Join-Path -Path $TargetDirectoryPath -ChildPath $dateDirectoryName

    if (!(Test-Path -Path $dateDirectoryPath -PathType Container))
    {
        Write-Verbose "Creating directory '$dateDirectoryPath'."
        New-Item -Path $dateDirectoryPath-ItemType Directory -Force > $null
    }

    [string] $filePath = $file.FullName
    Write-Information "Moving file '$filePath' into directory '$dateDirectoryPath'."
    Move-Item -Path $filePath -Destination $dateDirectoryPath
}