Search code examples
windowsfilecmdrenamebatch-processing

How to batch rename files with two extensions?


I have a file conversion tool that converts the files correctly but just appends the desired file type extension to the old filename. So example files are something like file1.ext1.ext2 . . . fileN.ext1.ext2

I thought it would be an easy fix - just use a wildcard operator on the ren command. example: ren \dir*.ext1.ext2 *.ext2

but the output remains .ext1.ext2. I feel like I'm close.

Any suggestions? I've seen suggestions to use push and pop but those seem unnecessary.

Thank you!


Solution

  • The * wildcard has a fair bit of compatibility handling related to DOS 8.3 names and whatnot. How did copying and renaming with wildcards work in MS-DOS?

    One way to get around it is to use full names without wildcards in the rename operation:

    @echo off
    
    REM test file:
    echo.>> "file1.foo.bar"
    
    REM remove foo extension:
    for %%A in (*.foo.bar) do for /F "eol=? delims=" %%B in ("%%~dpnA") do @ren "%%~fA" "%%~nB%%~xA"
    

    (Change %% to % if not in a batch file)