0

I have 200+ files in a single directory that I need to first search for in another directory with subs and replace after it is found. File names are the same. Content/size is usually different. Last time I had to do this I had a linux environment so I put together a command. I have no idea how to do this in windows.

  • Google directory synchronization or similar terms. There are lots of utilities that will do this, if I understand correctly. Depending on need, XCOPY might be sufficient. From a command prompt, type XCOPY /? for details. – Steve Rindsberg Jul 29 '14 at 19:10
  • Whenever I need this, I always use TeraCopy. Its a free copy program that supports testing for inconsistencies based on MD5 hashes. You copy the content from one folder to the other, and select Older only. It will then copy only the files that are different. A test at the end of the copy ensures that both locations are the same. If not, you can clean up the list of files to copy to only those that were different and copy those again. – LPChip Jul 29 '14 at 19:14
  • xcopy would work if I was just moving all files from X:\ to Y:\ what I need to do is search Y:\ for X:\file.txt and when the file Y:\sub\dir\file.txt is found replace it. – tnik Jul 29 '14 at 19:14
  • EDIT: I missed the part where the destination is in sub directories. Not sure this can be easily done in windows. – LPChip Jul 29 '14 at 19:15
  • replace command will work, I just have to figure out how to make it run through all the files in the x:\ directory. – tnik Jul 29 '14 at 19:34
0

I believe the following PowerShell script should do the job. It's probably far from optimal, since it's doing a full recursive search of the origin folder for each file, so you might want to look into tweaking it a bit. I strongly suggest testing it with non-critical data and folders first. I did some testing with a small set of sample files and it ran fine, but the files in your environment may not be so simple.

Note that there may be some unexpected behavior if a file you're looking for does not exist in the origin folder, or if there are multiple files in the origin folder which have the same name as the one being sought.

$DestDIR = 'C:\Destination'
$OrigDIR = 'C:\Origin'

Get-ChildItem $OrigDir | ForEach-Object {
    $OrigName = $_.Name
    $OrigPath = $_.FullName

    Get-ChildItem $DestDIR -Recurse | Where-Object {$_.Name -eq $OrigName} | ForEach-Object {
        $DestPath = $_.FullName
        Copy-Item $OrigPath $DestPath -Force
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.