Search code examples
loopsbatch-filemove

loop through all files in folder, check if first part of filename matches any files in a diff folder-move if not


I have 75,000 files for a karaoke system. For each song there's the music (.mp3) and the lyrics (.cdg). So, for each song there are two files.

Artist - SongName [Karaoke Brand1].mp3
Artist - SongName [Karaoke Brand1].cdg

The issue is that some songs have many versions (5-10). Brand1 is the best, so I've just used standard windows search to filter all files of that brand and cut them and pasted them manually which gave me 25,000 files in the new curated older.

But there are a lot of songs in the original folder that Brand1 doesn't have a version of. I prefer Brand2 and Brand3 in this case, but not every song is done by both. Regardless every song is at least done by one or the other.

I want to loop through all remaining MP3 files in (no need to look at ALL since the .cdgs are the same name with a different extension):

"D:/Karaoke/All" 

and and take the filename BEFORE the square brackets [wildcard?] and see if I already have a version of it in:

"D:/Karaoke/Curated"

If there is do nothing and move to next file.

If there isn't move Brand2 if it exists (full filename .mp3 AND .cdg) to D:/Karaoke/Curated.

If Brand2 doesn't exist, see if Brand3 does and use that one instead

The file naming is the same for all Brands, just the text inside the brackets [Brand] will be different.

EDIT what i've come up with so far

            @echo off
            setlocal enabledelayedexpansion
            for %%i in (*.mp3) do (
                for /f "tokens=1 delims=[" %%a in ("%%i") DO (
            REM get "checkable name first"
            REM     echo %%a
                    set checkName=%%a
            REM     echo !checkName!
                )
                echo !checkName!
            )
            exit

Solution

  • Here is a possible solution according to your requirements:

    @echo off
    
    cd /d "D:\Karaoke\All"
    for %%A IN ("*.mp3") do (
        for /f "delims=[]" %%B IN ("%%~nA") do (
            dir /A "D:\Karaoke\Curated\%%B*" >nul 2>&1
            if errorlevel 1 (
                if exist "%%B[Karaoke Brand2].mp3" (
                    move "%%B[Karaoke Brand2].*" "D:\Karaoke\Curated\"
                ) else (
                    if exist "%%B[Karaoke Brand3].mp3" (
                        move "%%B[Karaoke Brand3].*" "D:\Karaoke\Curated\"
                    )
                )
            )
        )
    )