Search code examples
windowsbatch-filefile-renamebatch-rename

Removing text from the middle of a file name with batch renaming


I'm trying to de-organize a series of video files named like this:

Something or Other.s1e1.mp4
Another title.s1e2.mp4
...
Yet More Titles.s1e87.mp4

And so on. What I want to do is strip out the "s1eNUM" part of the filename and restore it back to normal, so I'd get "Something or Other.mp4" and "Yet More Titles.mp4" and such.

Can this be done using Windows command line, with ren and batch files? Or do I need special software? Thanks!!


Solution

  • Use the for command.

    @echo off
    set YourFileName=title.s1e2.mp4
    for /f "tokens=1 delims=." %%x in ("%YourFileName%") do (set filename1=%%x)
    for /f "tokens=3 delims=." %%x in ("%YourFileName%") do (set filename2=%%x)
    echo %filename1%.%filename2%
    
    rename C:\PATH\%YourFileName% %filename1%.%filename2%
    
    pause>nul
    

    This should remove the "s1eNum" in the middle, then combine the file name and the extension at the end.