How to rename a set of files like this in DOS/Windows XP?

current file names:

file111.txt  
file112.txt  
file113.txt  

after renaming file names:

file0111.txt  
file0112.txt  
file0113.txt  

How can I achieve this?

link|improve this question

By "DOS" do you mean real MS-DOS, or the Windows "Command Prompt"? The latter is not DOS, but a Windows program with very different features. – grawity Apr 27 '11 at 10:45
no windows dos i will try your answer – Siva Apr 27 '11 at 10:52
...so do you mean Windows or DOS? Windows does not have DOS anymore. – grawity Apr 27 '11 at 10:55
feedback

1 Answer

up vote 1 down vote accepted

Batch script:

for %%f in (file???.txt) do call :ren %%f
goto :eof

:ren
    set name=%1
    ren "%name%" "%name:~0,4%0%name:~4%"

    :: Here, %name:~0,4% takes the first four characters, then you add a "0",
    :: and %name:~4% is everything after the fourth character.

Another possible way, which checks for all files starting with file100.txt and so on, so might be slower:

for /l %f in (100,1,999) do if exist "file%f.txt" ren "file%f.txt" "file0%f.txt"

(If you want to put this in a batch file, you need to change %f to %%f, same as in the first example.)

link|improve this answer
actually i am trying to do this.. rename all files - file.001 to file.1500 as file.0001 to file.1500... could u help? – Siva Apr 27 '11 at 10:56
@Siva: Use the first method in the answer. – grawity Apr 27 '11 at 10:57
for %%f in (file.???) do call :ren %%f goto :eof :ren set name=%1 ren "%name%" "%name:~0,4%0%name:~4%" :: Here, %name:~0,4% takes the first four characters, then you add a "0", :: and %name:~4% is everything after the fourth character. this is not executing.. am i missing something? – Siva Apr 27 '11 at 11:00
@Siva: 1) I don't understand "this is not executing". Could you give more details? 2) You need to put the entire text in a *.bat (or *.cmd) file and then run it. – grawity Apr 27 '11 at 11:01
yes i put in a .bat.. and i changed the file name from file???.txt to file.??? – Siva Apr 27 '11 at 11:03
show 1 more comment
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.