robertpas
robertpas

Reputation: 673

Mirroring folders with slight modifications

I have to "almost" mirror two folders. One folder contains the output from a tool with the file format *_output.txt. Files are continuously added here as the tool runs and it will produce hundreds of thousands of files. The other one should contain the same files as input for another tool, but that tool expects the format to be *_input.txt

My current solution is a powershell script that loops through the first folder, checks if the renamed file exists and if it doesn't, copies and renames it with Copy-Item. This, however, is proving very inefficient once the file number goes high enough. I would like to improve this.

Is it possible to somehow make use of robocopy's /MIR and also rename files in the second folder? I would like to prevent the original files being mirrored if a renamed file exists.

Is such a thing possible?

Upvotes: 0

Views: 230

Answers (1)

scaler
scaler

Reputation: 564

You could use FileSystemWatcher:

$watcher = New-Object -TypeName System.IO.FileSystemWatcher -Property @{Path='c:\temp\lib'; Filter='*_output.txt'; NotifyFilter=[IO.NotifyFilters]::FileName, [IO.NotifyFilters]::LastWrite};
Register-ObjectEvent -InputObject $watcher -EventName Created  -Action {Copy-Item $event.SourceEventArgs.FullPath $($event.SourceEventArgs.FullPath.Replace("output","input"))};
$watcher.EnableRaisingEvents = $true

Upvotes: 1

Related Questions