All answers I find are about deleting duplicate files, but I want to keep duplicate file names (not files, they only have the same name).

I have been playing around with RAW photo processing software. I have used two different programs to process a set of photo's. After using software A, I made a selection of the images I want to keep (and deleted the others). However, I like the processing from software B better.

So now I have a full set of files in C:\Temp\B\ (980 images), and a selection in C:\Temp\A\ (544 images)

Is there a way to filter out the images I have not selected in A and delete those files from C:\Temp\B\?

I'm using Windows 10, I'm fine with using a bit of scripting in bash (using babun) or Python.


My solution is an adaptation of https://stackoverflow.com/questions/1995373/deleting-all-files-in-a-directory-with-python

I have run this in Visual Studio Code, using the Python extension

import os a = os.listdir("C:/Data/A") b = os.listdir("C:/Data/B") for f in b: if f not in a: print(f) os.remove(os.path.join("C:/Data/B", f))

  • Nice, but seeing as you loop over all the file to generate the list, why not delete them right then and there? I.e. instead of going over all the files and adding the ones you want to the list, go over the files and delete the ones you want. – Tomer Godinger Oct 23 '17 at 16:07
  • @TomerGodinger Good point, I do not need the second for loop. I have updated the answer to reflect this change. – RedPixel Oct 24 '17 at 7:36

You can save the following line into a new batch file (.cmd or .bat):

FOR %%A in (C:\Temp\A\*) DO MOVE C:\Temp\B\%%~nxA C:\Temp\X\

Now you have all the files from A, which are present in B, to be moved from B into a new folder X. Do a visual inspection to ensure that everything is OK, and then delete B manually.

You can also run that command from CMD, but you must leave a single percent sign instead of two.

Be very, very careful, as a single missing character can eat all your B files. I ran the command like this first, to make sure that it is safe:

FOR %%A in (C:\Temp\A\*) DO ECHO MOVE C:\Temp\B\%%~nxA C:\Temp\X\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.