Reputation: 4639
I have a folder (dated with yesterday's date) on our R Drive for each day's reports.
These are copied over each morning from our J Drive, to which only some people have access. I do the copy with PowerShell but am fairly new to it, so have always been too scared to use MOVE instead of COPY, and to delete the originals from the J Drive. So I do that manually after ensuring the copy was successful.
What I want to do is: find all files (they are txt and ZIP) in the R Drive folder, that ALSO exist on the J Drive, and if their sizes are the same, delete the ones on the J Drive.
What is the best way of doing this? I'd prefer to not handle each file separately.
Upvotes: 2
Views: 7715
Reputation: 1
if you copy them all to the same folder then windows would want to put a (1) at the end of the dup files(if they have the same name) and then you could run
Get-ChildItem 'C:\Your\Path*(1)*' | Remove-Item
just worked for me
Upvotes: 0
Reputation: 720
To answer your question, here's an example of how I've done something similar in the past:
$one = Get-ChildItem C:\temp\test1
$two = Get-ChildItem C:\temp\test2
$matches = (Compare-Object -ReferenceObject $one -DifferenceObject $two -Property Name,Length -ExcludeDifferent -IncludeEqual)
foreach ($file in $matches)
{
Remove-Item c:\temp\test2\$($file.Name)
}
I suspect you may want to add some logging to this, maybe even prompt for deletion if you're the extremely nervous type. :)
Upvotes: 4