I have two disks which has the same directory structure. C:\Files
and D:\Files
Both C:\Files
and D:\Files
have multiple directories under them but have the same name etc, but the files inside them differ in extension. In C:\Files
they are *.csv
and in D:\Files
, a process monitors the files (copied from C:\
) and once it is done changes the files to *.processed
.
I want a script that would do that copy. I.e copy files from C:\Files
to D:\Files
which have not been processed by comparing only the file names.
You want something like this. The property you want to compare on is called BaseName
which powershell helpfully adds for you to the IO.FileSystemInfo
class (FileInfo.BaseName exists in PowerShell but not in straight .NET). BaseName
is just the name of the file, and doesn't contain any of the extensions that you don't care about.
$sourceFiles = Get-ChildItem C:\files -Recurse -File -Filter "*.csv"
$destinationFiles = Get-ChildItem D:\files -Recurse -File
foreach($sourceFile in $sourceFiles) {
$exists = $destinationFiles | Where-Object {$_.BaseName -eq $sourceFile.BaseName}
if(!$exists) {
Copy-Item $sourceFile.fullname -Destination "D:\Files\$($sourceFile.BaseName)"
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments