Is there a way to remove files from a folder that are in another folder?
There's free software out there called WinMerge. You can use this software to match up duplicates. First, use File
→ Open
, and choose both directories, with the folder with files you want to keep on the left, and those you do not on the right. Then, go to View
, and deselect Show Different Items
, Show Left Unique Items
, and Show Right Unique Items
. This will leave just the identical files left in the list. After that, choose Edit
→ Select All
, right-click on any file, and click on Delete
→ Right
. This will delete the duplicates from the right-hand folder.
This can be done through the commandline by using the command forfiles
Lets assume you have Folder A located in c:\temp\Folder A
, and Folder B located in c:\temp\Folder B
The command would then be:
c:\>forfiles /p "c:\temp\Folder A" /c "cmd /c del c:\temp\Folder B\@file"
After this is done, Folder B will have all files removed that are present in Folder A. Keep in mind, that if folder B has files with the same name, but not the same content, they will still be deleted.
It is possible to extend this to work with folders in subfolders too, but out of fear for this to become unnecessary complicated, I've decided against posting it. It would require the /s and @relpath options (and further testing xD)
You can use this PowerShell script:
$folderA = 'C:\Users\Ben\test\a\' # Folder to remove cross-folder duplicates from
$folderB = 'C:\Users\Ben\test\b\' # Folder to keep the last remaining copies in
Get-ChildItem $folderB | ForEach-Object {
$pathInA = $folderA + $_.Name
If (Test-Path $pathInA) {Remove-Item $pathInA}
}
Hopefully it's fairly self-explanatory. It looks at every item in Folder B, checks whether there's an item with the same name in Folder A, and if so, it removes the Folder A item. Note that the final \
in the folder paths is important.
One-line version:
gci 'C:\Users\Ben\test\b\' | % {del ('C:\Users\Ben\test\a\' + $_.Name) -EA 'SilentlyContinue'}
If you don't care whether you get a deluge of red errors in the console, you can remove the -EA 'SilentlyContinue'
.
Save it as a .ps1
file, e.g. dedupe.ps1
. Before you can run PowerShell scripts, you'll need to enable their execution:
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
Then you'll be able to invoke it with .\dedupe.ps1
when you're in the folder that contains it.