0

I want to keep only the newest version of the files with the same name in a folder and subfolders. In other words, I want to remove duplicate files in a folder and subfolders, considering only that two files are duplicates when they have the same name (and extension) and keep only the one that has the newest modification date.

I know I can do it manually with programs like CCleaner or Nirsoft's SearchMyFiles but they don't have the posibility to select all but the newest one for deleting them, you have to do it manually.

So is there a command line tool, or cmd bat or powershell to accomplish this task?

  • 2
    Since you cannot have more than one file of the same name in a folder, do you mean files of the same name in sub folders of a folder? – Michael Harvey Sep 12 '18 at 20:04
  • Do you already know the 'same name' that you want to delete? – Michael Harvey Sep 12 '18 at 20:10
  • Do the "same name" files have different file extensions? – glenn jackman Sep 12 '18 at 20:11
  • Yes, sorry, I mean in the folder and subfolders, and I mean same name and extension. I want to remove duplicate files, considering only that two files are duplicates when the have the same name (and extension) and keep only the newest one. – liamZ Sep 12 '18 at 21:00
  • writing a file for you since I need this too. – Mark Deven Sep 15 '18 at 17:22
0

I made a program for you here: https://github.com/ITCMD/Remove-Duplicates

DelDupe /? output:

Deletes duplicate Files except the latest edited.

DelDupe  "Directory" "Filename" /F /C Character

Directory:      Directory to search for duplicate names. Use C:\Users\IT Command\Documents\Batch Codes\Keep Recent Mod Date for current dir.
Filename:       Duplicated Filename to search for
/F              Do not prompt removal
/C              Provide your own system character: The default character it uses to seperate files is ~. You don't need to know what this means, but if you have the ~ character in your filename then supply your own.
Character:      The Character you want to use instead of ~

Example:
  DelDupe "C:\Users\IT Command\Documents\Batch Codes\Keep Recent Mod Date" "Picture.png"

This example will surch all subdirectories in your current folder for a file called "Picture.png" and will delete all copies except for the most recently modified one.

Created by Lucas Elliott   www.itcommand.tech

Note that this is not a site to ask people to make full scripts for you. People get mad if you don't show that you've tried (I know you probably don't know where to start). It's okay to ask a question like you did in my opinion, but don't always expect a full answer. I just needed this for me anyway.

| improve this answer | |
  • Hi, thanks a lot for your answer and the script. But the file name is not a parameter in my question. I want to keep only last version of every file in the tree of directories. I can't test it right now, but do you think something like: forfiles /p folder /s /c "cmd /c DelDupe folder @path" will work? – liamZ Sep 17 '18 at 17:22
  • Anyway, I thought this was a common task and there would be some command line tool (exe) but the ones I found like FindDupe or Dupemerge are for exact duplicates and remove them or hardlink them. About the GUI tools like CCleaner and SearchMyFiles, I finally found one that automatically keep only last version for all the files with the same name: CloneSpy. But I prefer to do it from the command line. So thanks again for your code and I'll see how to do it with it. – liamZ Sep 17 '18 at 18:04
  • glad to hear it! – Mark Deven Sep 17 '18 at 18:07
  • Hi, I'm testing DelDupe and I had to modify line 56 to: if /i not defined force. But I'm sorry to say that it does not keep the last modified file. It keeps one of the other files. – liamZ Sep 17 '18 at 21:35
  • Hmmm I’ll take a look sorry about that. – Mark Deven Sep 18 '18 at 0:31

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.