用于搜索文件和重命名文件的脚本

时间:2012-07-20 10:37:55

标签: windows scripting file-rename

我在以下位置\\mastercorrespondence的数百个不同的文件夹,子文件夹和子子文件夹中有大约11000个不同的文件,我需要重命名一些文件并每天从K:\CDS_TOOL_MANUAL_OVERRIDES复制相应的文件在他们自己的子文件夹中。

简而言之,它应该执行以下步骤

  1. K:\ CDS_TOOL_MANUAL_OVERRIDES文件夹中查找任何PDF格式的文档。

  2. 对于K:\ CDS_TOOL_MANUAL_OVERRIDES中的每个文档,在\\mastercorrespondence”任意子目录中查找具有相同文件名的PDF文档。

  3. 如果找到相应的文件,则将\\mastercorrespondence子目录中的文件重命名为<Original Filename>_<Overwritten>_<dd.mm.yy>

  4. 将文件从K:\ CDS_TOOL_MANUAL_OVERRIDES文件夹移动到与\\10.5.13.10\mastercorrespondence子目录中对应的位置。

  5. 如果任何文件在\\mastercorrespondence子目录中没有相应的文件,则写一条消息到日志文件,说明不匹配文件的名称。

  6. 文件夹结构就像。

    \\mastercorrespondence\SIPP\21\201201\01
    \\mastercorrespondence\SIPP\21\2012022
    \\mastercorrespondence\ISA\10201201\201202\02
    \\mastercorrespondence\ISA\10201201\201203
    \\mastercorrespondence\ISA\10201201\201204
    \\mastercorrespondence\ISA\10201201\201205
    

1 个答案:

答案 0 :(得分:0)

这是PowerShell的一个起点,如果它适合你:

#Look for any PDF format documents in K:\ CDS_TOOL_MANUAL_OVERRIDES folder.
$pdfFiles = Get-ChildItem -Path K:\CDS_TOOL_MANUAL_OVERRIDES -filter "*.pdf"

#For each document in K:\ CDS_TOOL_MANUAL_OVERRIDES look for PDF document with identical file name held in the \\mastercorrespondence” any sub-directory.
$referenceFiles = Get-ChildItem -Path \\mastercorrespondence -recurse -filter "*.pdf"
$pdfFiles | %{
    $_ = $pdfFile
    $matched = ""
    #I don't fully understand what you're asking for, but it seems to me that the move and rename are on the same file, and technically a rename is a move... so I've combined them.
    $referenceFiles | %{if ($_.Name -eq $pdfFile.Name) {$matched = $_}  }
    if ($matched -ne "") {
        $destinationName = ($pdfFile.Name + "_OverWritten_"+(Get-Date -format dd.MM.yy))
        Move-Item -Path $_.FullName -Destination ($matched.DirectoryName + "\" + $destinationName)
    }
    else{
        #If any documents did not have a corresponding file in \\mastercorrespondence sub-directory then write a message to log file stating names of unmatched files.
        ("Unable to locate matching file: " + $pdfFile.FullName) | Out-File -Append -FilePath "K:\mover.log"
    }
}