user2725402
user2725402

Reputation: 4639

Compare folders and delete duplicates

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

Answers (3)

user3854885
user3854885

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

crownedjitter
crownedjitter

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

foxidrive
foxidrive

Reputation: 41234

Just use MOVE. It's as secure as using copy.

Upvotes: 1

Related Questions