【问题标题】:Powershell script to move files into year/month folders based on creation timestampPowershell脚本根据创建时间戳将文件移动到年/月文件夹中
【发布时间】:2014-02-01 22:13:54
【问题描述】:

这里的第一篇文章...如果格式不正确,我深表歉意...我会努力解决这个问题。我正在编写一个 Powershell 脚本,该脚本将为我提供以下信息,以便我可以使用另一个到目前为止完美运行的批处理文件。随着我对 Powershell 理解的加深...我很可能会更改批处理文件,因为它很长。

TL:DR Newb 使用 Powershell 需要帮助

我希望 Powershell 为文件夹中的每个文件输出一行信息,不包括任何子文件夹。我希望我的 txt 文件看起来像这样:

文件创建日期_文件名的完整路径

每行一个文件。稍后会解析成文本文件

这是我到目前为止所拥有的......似乎我只需要一个 for 循环来运行类似这个伪代码的东西,我应该很高兴。在这一点上,任何帮助将不胜感激。

谢谢大家,我希望我没有用格式化杀死你。

$USS_Folder="path\USS_Files\"
$uss_year=#4 digit year of file creation date
$uss_month=#2 digit year of file creation date
$uss_file=# Full filename including path and Creation_Date

New-Item -ItemType directory -Path $USS_Folder\$uss_year\$uss_month

Move-Item $uss_file $USS_Folder\$uss_year\$uss_month

【问题讨论】:

    标签: powershell


    【解决方案1】:

    因此,我花了一段时间浏览了许多充满脚本的页面,然后在下面提出了这个解决方案,并根据我的需要进行了修改。我已将其用于生产,效果很好。

    改编自PowerShell: Moving files into subfolder based on date

    <#
    Set Variables of Source folder and Destination folder
    Assign variable of files to be the files with uss extension
    For each file with uss extension assign the Directory variable the information for file creation year and month
        if the year and month folder do not exist, then create them from file creation information
    Move file to sub-folder of year and month from file creation information passed into Directory variable
    #>
    
    $SourceDir = "path to uss files\USS_Files\"
    $DestinationDir = "path to uss files\USS_Files\"
    
    $files = get-childitem $SourceDir *.uss
    
    foreach ($file in $files) 
    {
        $Directory = $DestinationDir + "" + $file.CreationTime.Date.ToString('yyyy') + "\" + $file.CreationTime.Date.ToString('MM-MMM')
    
        if (!(Test-Path $Directory))
        {
            New-Item $directory -type directory
        }
        Move-Item $file.fullname $Directory 
    }
    

    【讨论】:

    • 如果你有数千个文件,测试路径是非常必要的。谢谢!
    • 很抱歉这么久才恢复...我根据需要对此进行了一些编辑(在所有子文件夹和任何扩展名中获取所有文件)并且工作正常,但是当我遇到问题时文件名中包含[]。我怎样才能转义这些字符,以便脚本不会为他们失败?我没有使用powershell的经验,所以请非常具体。谢谢!
    • @sl0815 如果您有数千个文件,是否必要?为什么?
    • @Kiquenet 因为脚本不会尝试创建可能已经存在的目录(如此处列出的其他解决方案)。这大大加快了速度。在大多数情况下,只需要创建几个目录(这对我来说就是重点),然后大多数对 new-item 的调用都会被跳过。
    【解决方案2】:

    假设您使用它来将照片排列到文件夹中,并且您的文件名以 YYYYMMDD 开头,那么以下脚本运行良好。此方法优于上述方法,因为我们复制/粘贴/移动,因此创建和修改日期与采用的日期不同。

    另外,要运行此脚本,只需粘贴到扩展名为 .ps1 的文本文件中,右键单击该文件,然后选择使用 powershell 运行。

    # Get the files which should be moved, without folders
    $files = Get-ChildItem 'C:\Users\YourName\SourceFolder\YourPhotosFolder' -Recurse | where {!$_.PsIsContainer}
    
    # List Files which will be moved
    $files
    
    # Target Filder where files should be moved to. The script will automatically create a folder for the year and month.
    $targetPath = 'C:\Users\YourName\SourceFolder\YourAlbumnsFolder'
    
    foreach ($file in $files)
    {
    # Get year and Month of the file using the filename
    $bn = $file.basename.ToString()
    $year = $bn.substring(0,4)
    $month = $bn.substring(4,2)
    $day = $bn.substring(6,2)
    
    # Out FileName, year and month
    $file.Name
    $year
    $month
    $day
    
    # Set Directory Path
    $Directory = $targetPath + "\" + $year + "\" + $day + "-" + $month + "-" + $year
    # Create directory if it doesn't exsist
    if (!(Test-Path $Directory))
    {
    New-Item $directory -type directory
    }
    
    # Move File to new location
    $file | Move-Item -Destination $Directory
    }
    

    如果你想使用最后修改的日期,那么使用下面的(之前代码的略微修改的版本)

    # Get the files which should be moved, without folders
    $files = Get-ChildItem 'C:\Users\YourName\SourceFolder\YourPhotosFolder' -Recurse | where {!$_.PsIsContainer}
    
    # List Files which will be moved
    $files
    
    # Target Filder where files should be moved to. The script will automatically create a folder for the year and month.
    $targetPath = 'C:\Users\YourName\SourceFolder\YourAlbumnsFolder'
    
    foreach ($file in $files)
    {
    # Get year and Month of the file
    # I used LastWriteTime since this are synced files and the creation day will be the date when it was synced
    $year = $file.LastWriteTime.Year.ToString()
    $month = $file.LastWriteTime.Month.ToString()
    $day = $file.LastWriteTime.Day.ToString()
    
    # Out FileName, year and month
    $file.Name
    $year
    $month
    $day
    
    # Set Directory Path
    $Directory = $targetPath + "\" + $year + "\" + $day + "-" + $month + "-" + $year
    # Create directory if it doesn't exsist
    if (!(Test-Path $Directory))
    {
    New-Item $directory -type directory
    }
    
    # Move File to new location
    $file | Move-Item -Destination $Directory
    }
    

    如果你想使用创建日期然后使用下面的(之前代码的稍微修改的版本)

    # Get the files which should be moved, without folders
    $files = Get-ChildItem 'C:\Users\YourName\SourceFolder\YourPhotosFolder' -Recurse | where {!$_.PsIsContainer}
    
    # List Files which will be moved
    $files
    
    # Target Filder where files should be moved to. The script will automatically create a folder for the year and month.
    $targetPath = 'C:\Users\YourName\SourceFolder\YourAlbumnsFolder'
    
    foreach ($file in $files)
    {
    # Get year and Month of the file
    # I used LastWriteTime since this are synced files and the creation day will be the date when it was synced
    $year = $file.CreationTime.Year.ToString()
    $month = $file.CreationTime.Month.ToString()
    $day = $file.CreationTime.Day.ToString()
    $hour = $file.CreationTime.Hour.ToString()
    
    # Out FileName, year and month
    $file.Name
    $year
    $month
    $day
    
    # Set Directory Path
    $Directory = $targetPath + "\" + $year + "\" + $day + "-" + $month + "-" + $year
    # Create directory if it doesn't exsist
    if (!(Test-Path $Directory))
    {
    New-Item $directory -type directory
    }
    
    # Move File to new location
    $file | Move-Item -Destination $Directory
    }
    

    GitHub上的整个项目代码

    【讨论】:

    • 你为什么要命名它们为 $day-$month-$year?他们不会那样按时间顺序排序。
    【解决方案3】:

    像这样?

    $USS_Folder = "path\USS_Files"
    
    get-childitem | % {
    
        $file = $_.FullName 
        $date = Get-Date ($_.CreationTime)
        $month = $date.month
        $year = $date.year
    
        new-item -type Directory -path "$USS_Folder\$year\$month"
        move-item $file "$USS_Folder\$year\$month"
    }
    

    【讨论】:

      【解决方案4】:

      这是我在我的环境中使用但已转换为您的要求的解决方案:

       $USS_Folder = "path\USS_Files\"
       Get-ChildItem -File| Sort-Object LastWriteTime |`
        Group {$_.LastWriteTime.ToString("yyyy-MM")} |`
        % {
          $folder = $USS_Folder + $_.name 
          if (!(Test-Path $folder)) `
            {new-item -type Directory -path $folder -ErrorAction SilentlyContinue}
          $_.group|move-item -Destination $folder
          }
      

      【讨论】:

        猜你喜欢
        • 2023-03-08
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多