Search code examples
windowsbatch-filedos

Rename Multiple files with in Dos batch file


I wish to rename all files inside the folder *.txt, so the result will be "1.txt", "2.txt" and "3.txt", ....

How can I do so?


Solution

  • The following may accomplish what you are looking for. It uses a for loop to iterate through the text files and makes a "call" to another bit of the batch file to do the rename and increment of a variable.

    Edit Change math operation to cleaner solution suggested by Andriy.

    @echo off
    set i=1
    for %%f in (*.txt) do call :renameit "%%f"
    goto done
    
    :renameit
    ren %1 %i%.txt
    set /A i+=1
    
    :done