CSA
CSA

Reputation: 43

batch file - detect duplicate file names before char?

i have 70000 karaoke songs in folder "D:\Karaoke\". each one has two files

artist - songname [GT karaoke].mp3
artist - songname [GT karaoke].cdg
artist - songname [SJ karaoke].mp3
artist - songname [SJ karaoke].cdg
artist - songname [AF karaoke].mp3
artist - songname [AF karaoke].cdg

assuming all the files above represent the same song. I want to KEEP the AF version where it is and move all other files that match to a new folder called "D:\Karaoke\duplicates\". If no AF version exists then do nothing. would need to grab the file name before the [AF Karaoke] because its unique after that point.

Upvotes: 0

Views: 138

Answers (2)

aschipfl
aschipfl

Reputation: 34899

Well, I would saddle the horse from the other side and begin searching for (.mp3) files whose names contain [AF karaoke] and if found then do something else do not ‐ like this, given that all the file names contain exactly one pair of brackets ([ and ]):

@echo off
rem // Change to working directory:
pushd "D:\Karaoke" && (
    rem // Create sub-directory for dublicate items if not yet done:
    2> nul mkdir "duplicates"
    rem // Enumerate `.mp3` files whose names contain `[AF karaoke]`:
    for /F "tokens=1-3 delims=[] eol=]" %%A in ('dir /B /A:-D-H-S "*[AF karaoke].mp3"') do (
        rem // Enumerate all files whose partial names before `[` are the same:
        for /F "tokens=1-3 delims=[] eol=]" %%D in ('dir /B /A:-D-H-S "%%A[?? karaoke].*"') do (
            rem // Exclude files whose names contain `[AF karaoke]` and move the rest:
            if /I not "%%E"=="AF karaoke" ECHO move "%%D[%%E]%%F" "duplicates\"
        )
    )
    rem // Return to original directory:
    popd
)

This script does not really care about the file extensions (like .mp3, .cdg), neither does it regard the beginnings of the strings in between brackets other than AF (like GT, SJ).

After having tested for the correct output, remove the upper-case ECHO command in front of the move command!

Upvotes: 1

Io-oI
Io-oI

Reputation: 2565

If you are using command/line in same folder where these files are, try this:


For %F in (*)do echo/%~nF|find /v /i "[AF k" >nul && move "%~dpnxF" "D:\Karaoke\duplicates\" >nul

For bat file:


@echo off && cd /d "d:\folder\where\files\are"
For %%F in (*)do echo/%%~nF|find /v /i "[AF k" >nul && move "%%~dpnxF" "D:\Karaoke\duplicates\" >nul

Upvotes: 0

Related Questions