jeff abc
jeff abc

Reputation: 3

Script to search for files and rename files

I have about 11000 different files in hundreds different folders, sub folders and sub-sub folders in following location \\mastercorrespondence, and I need to rename some of the files and copy corresponding file from K:\CDS_TOOL_MANUAL_OVERRIDES daily in their own subfolder.

In short it should perform following steps

  1. Look for any PDF format documents in K:\ CDS_TOOL_MANUAL_OVERRIDES folder.

  2. For each document in K:\ CDS_TOOL_MANUAL_OVERRIDES look for PDF document with identical file name held in the \\mastercorrespondence” any sub-directory.

  3. If corresponding file found then rename file in \\mastercorrespondence sub-directory as <Original Filename>_<Overwritten>_<dd.mm.yy>

  4. Move the file from K:\ CDS_TOOL_MANUAL_OVERRIDES folder to the same location as it counterpart in the \\10.5.13.10\mastercorrespondence sub-directory.

  5. 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.

Folder Structure is Like.

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

Upvotes: 0

Views: 416

Answers (1)

Chris N
Chris N

Reputation: 7479

Here's a starting point in PowerShell, if that works for you:

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

Upvotes: 0

Related Questions