Search code examples
windowscommand-linerenamebatch-renamewindows-client

Use the rename function to modify multiple parts of a filename?


I've got the following pattern :

[AAAAAA] Title - 1234 (AAAAAA)[Something that changes].mkv

I'd like to only keep : Title - 1234

The AAAAAA parts are always the same : 10 letters for the first set, and 4 numbers + 1 letter for the second set.

The Title - 1234 part changes constantly. The [Something that changes] part changes constantly.

Is there a way to remove what's before ] and after (, so that I only keep the Title and the Episode n° ?

I don't mind making multiple "rename" commands (I do that in batch anyway)

Any way to do that ?

Thanks !


Solution

  • use this string substitute:

    This find the first "]" and trim everything from beginning Then trim all after the first "("

    @echo off
    
    setlocal enabledelayedexpansion
    
    for %%F in (*.mkv) do (
       set "f=%%~F"
       set "f=!f:*]=!"
       call :trim
       echo ren "%%~F" "!f!%%~xF"
    )
    
    pause
    goto :eof
    
    :trim
    SET f=%f:(=&rem.%
    goto :eof
    
    

    output:

    C:\Users\fposc\AppData\Local\Temp>ren1
    ren "[AAAAAA] Title - 1234 (AAAAAA)[Something that changes].mkv" " Title - 1234 .mkv"
    Premere un tasto per continuare . . .
    

    If you want to remove leading and trailing spaces you can include them in the code like this:

    @echo off
    
    setlocal enabledelayedexpansion
    
    for %%F in (*.mkv) do (
       set "f=%%~F"
       set "f=!f:*] =!"
       call :trim
       echo ren "%%~F" "!f!%%~xF"
    )
    
    pause
    goto :eof
    
    :trim
    SET f=%f: (=&rem.%
    goto :eof
    

    output:

    C:\Users\fposc\AppData\Local\Temp>ren1
    ren "[AAAAAA] Title - 1234 (AAAAAA)[Something that changes].mkv" "Title - 1234.mkv"
    Premere un tasto per continuare . . .
    

    Test this and when is ok you can remove the "echo" and left "ren" command or copy the output in a file and then execute.