Delete files which have almost identical filenames EXCEPT last word (Windows)

Posted on

QUESTION :

I’m trying to sort a directory of files with some conditions but I can’t for the life of me figure this out. Here’s a little example of some filenames which are to be sorted out (deleted).

Turok 3 - Shadow of Oblivion (USA).zip
Turok 3 - Shadow of Oblivion (Europe).zip
Duke Nukem - Zero Hour (Europe).zip
Duke Nukem - Zero Hour (USA).zip
Gex 64 - Enter the Gecko (Europe).zip
NBA Jam 2000 (Europe).zip

I’m trying to keep one edition of each file, but there’s a catch which makes this hard. In the above case I would want to keep the “Gex 64” and “NBA Jam” files because they are the only ones there. The “Turok” and “Duke Nukem” got two editions each, USA and Europe, and here I want to keep just the Europe ones.

I can’t do this manually because we are talking about thousand of files. Is there a software for this, or is the command line powerful enough to have a batch file do this?

ANSWER :

Batch script

The script can handle up multiple extensions, and will default to the current directory if none is specified in the first parameter.

Note As-is, the script will just list the files that should be deleted, but won’t actually delete them. When you’re sure the list is correct, replace the REM del /f "%%~B" line in the :deleteDuplicates subroutine with del /f "%%~B". Work on a copy first to ensure there are no issues.

@echo off
setlocal
setlocal enabledelayedexpansion

REM ensure the target directory exists
if "%~1" == "" (
set targetDir=%~dp0
) else (
if not exist "%~1" exit /b 2

Leave a Reply

Your email address will not be published.