Powershell:根据创建日期

时间:2016-10-08 22:51:53

标签: powershell

我不是编码员,但我仍然试图调整此处找到的PS脚本,但仍然无法获得我想要的行为。对我来说,最困难的部分是2位数的日期要求(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."

3 个答案:

答案 0 :(得分:3)

[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 ”的值也是您要存储匹配图片的目标文件夹。

修改

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 
    }
}

答案 1 :(得分:0)

扩展更多以隐藏重复项

$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 
}

答案 2 :(得分:0)

我最终创建了一个脚本,该脚本可以完全满足您的要求。您可以find it in GitHub here,它将获得最新版本的代码。

这是当前的实现,为简洁起见对其进行了编辑,删除了不必要的功能,并针对问题的需要进行了量身定制:

[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可以扫描文件两次,遍历刚移动的文件。