Search code examples
batch-filecmdbatch-rename

Removing second extension from file name


I have files like below:

Filename.txt
Filename.txt.new
Filename2.txt
Filename2.txt.new

I want to remove the "new" keyword for the files ending with "new"

also when we do this, existing file will also have same name, so i want to overwrite the same with the contents of the files ending with "new"


Solution

  • Sadly this is a case where ren *.foo *.bar won't work, but we can do it with a simple loop:

    for %x in (*.new) do move /y "%x" "%~nx"
    

    This simply loops over all files that have a .new extension (you could also do the same only for *.txt.new) and renames them. %~nx removes the extension from the name, in this case the .new.

    When using this in a batch file you have to double the % signs:

    for %%x in (*.new) do move /y "%%x" "%%~nx"