删除文件的所有修订版本(最新修订版本除外)

删除文件的所有修订版本(最新修订版本除外)

因此,我有大量工作图纸列表,我希望能够将它们全部转储到一个文件夹中,然后运行批处理,删除所有较旧的版本,只保留最高版本。我甚至不确定如果没有深入的编程,这是否可行,所以我想在这里问一下。

文件名示例:

  • 01-XY-001-Rev-0_1-6-2014.pdf
  • 01-XY-001-Rev-2_1-13-2014.pdf
  • 01-XY-001-Rev-9_2-1-2014.pdf
  • 01-XY-001-修订-11_2-4-2014.pdf
  • 01-XY-002-Rev-0_1-7-2014.pdf
  • 01-XY-002-Rev-4_1-13-2014.pdf
  • 01-XY-002-Rev-7_1-26-2014.pdf
  • 01-XY-002-Rev-11_2-4-2014.pdf
  • 01-XXX-001-修订-0_1-13-2014.pdf
  • 01-XXX-001-Rev-4_1-21-2014.pdf
  • 01-XXX-001-修订-6_2-1-2014.pdf
  • 01-XXX-001-修订-10_2-4-2014.pdf

最后,我希望它看起来像:

  • 01-XY-001-修订-11_2-4-2014.pdf
  • 01-XY-002-Rev-11_2-4-2014.pdf
  • 01-XXX-001-修订-10_2-4-2014.pdf

等等。考虑到有数百个文件具有不同的名称,这可能吗?唯一一致的是 Rev-1、Rev-2、Rev-3 等。其余的则根据图纸如上所示发生变化。我真的不认为这有可能,但我还是愿意问。

答案1

我们不是脚本编写服务,但是我有一些时间和兴趣,所以您可以使用 PowerShell 脚本:

#Set directory to search (. = current directory).
$dir = "."

#Get a list of all the files (only), sorted with newest on top.
$dirFiles = Get-ChildItem -Path $dir | where { ! $_.PSIsContainer } | Sort-Object LastAccessTime -Descending

#Create an array to hold unique file name parts.
$uniqueFileNameParts = @()

#Create an array to hold final file list of files to keep.
$filesToKeep = @()

#Add the file name of the script itself to the files to keep, to prevent it from being deleted if it's in the same folder you're trying to clean.
$filesToKeep += $MyInvocation.MyCommand.Name

#Loop through all the files in the directory list.
foreach ($file in $dirFiles) {
    #If it contains "-Rev-" pull the first part of the file name (up to and including "-Rev-").
    $filenameTokenLocation = $file.name.IndexOf("-Rev-")
    if ($filenameTokenLocation -ge 0) {
        $endOfString = $filenameTokenLocation + 5
        $subString = $file.name.Substring(0,$endOfString)

        #If the file name part doesn't already exist in the array, add it to it.
        if ($uniqueFileNameParts -notcontains $subString) {
            $uniqueFileNameParts += $subString
        } 
    }
}

#Loop through all the file name parts.
foreach ($fileName in $uniqueFileNameParts) {
    #Create a list of all files starting with that file name part, select the one file with the newest "LastWriteTime" attribute, and assign it to $latest.
    $latest = Get-ChildItem -Path $dir | where { ! $_.PSIsContainer } | where {  $_.name.StartsWith($fileName) } | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    #Add that file to the list of files to keep.
    $filesToKeep += $latest.name
}

#Get all files in the folder that are not in the list of files to keep, and remove them.
Get-ChildItem -exclude ($filesToKeep) | where { ! $_.PSIsContainer } | Remove-Item

笔记:

  • 它使用文件的上次写入时间来确定哪个是“最新的”,而不考虑文件名本身的时间/日期戳。
  • 它区分大小写,因此文件名称XYZ.txt不一定与名称相同的文件名称xYz.TxT
  • 它不是递归的,它只检查您瞄准的文件夹/目录,而忽略子文件夹。
  • 这是非常危险的,所以在尝试之前请先备份文件夹。:)

希望有帮助!

相关内容