Search code examples
windowsbatch-filebatch-rename

Renaming parts of files with Windows Batch


How could i rename a certain part of all files inside a directory.

Example

[HorribleSubs] File1 [720p].mkv
[HorribleSubs] File2 [1080p].mkv

Must be renamed into

File1.mkv
File2.mkv

Solution

  • @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION 
    SET "sourcedir=U:\sourcedir"
    FOR /f "tokens=2delims=[]" %%a IN (
     'dir /b /a-d "%sourcedir%\[*]*[*].mkv" '
     ) DO (
     SET "newname=%%a"
     ECHO(REN "%sourcedir%\[*]%%a[*].mkv" "!newname:~1,-1!.mkv"
    )
    
    GOTO :EOF
    

    This should fix your problem.

    The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.