windowsbatch-filecmdbatch-rename

MS Batch, Rename files by pattern in Excel or CSV list


I have documents with names like:

foo_baar_AB_01.ending
fOo_BaAr_BC_05.ending
FOo_baaR_BA_15.ending

And a Excel or CSV List with a ruleset to Rename the Files:

AB ; Data
BC ; Stuff
BA ; Other

My Task is to rename the Files. The Result should look like:

foo_baar_AB_01.Data.ending
fOo_BaAr_BC_05.Stuff.ending
FOo_baaR_BA_15.Other.ending

Is there a Solution to Integrate the Exel-Pattern-List in the batch file or do I have to integrate the Pattern-List in the batchfile? And what would be a Solution for this Problem?


Solution

  • @echo off
    setlocal EnableDelayedExpansion
    
    rem Load the list of names from the ruleset
    for /F "tokens=1,2 delims=; " %%a in (list.csv) do (
       set "name[%%a]=%%b"
    )
    
    rem Process the files
    for /F "tokens=1-5 delims=_." %%a in ('dir /B /A-D *.ending') do (
       ECHO ren "%%a_%%b_%%c_%%d.%%e" "%%a_%%b_%%c_%%d.!name[%%c]!.%%e"
    )
    

    After confirmed that the names are correct, remove the ECHO part from the ren command.