DOS Shell script to rename multiple files in one line
I needed to prefix a long list of files with a number, so I looked around and did not find much info, so I thought it might be useful for someone if I post it here (yes, some of us are still using Windows).
Here is how to prefix files:
for %i in (*.*) do (ren "%i" "prefix %i")
And here is how to prefix folders:
for /D %i in (*.*) do (ren "%i" "prefix %i")
The whole For syntax is explained on Microsoft’s web site: For in batch files.
Comments
-
that was useful, i needed to suffix something and this almost worked
for %i in (*.*) do (ren "%i" "%i_suffix.abc")
problem with this is that the file extension gets included in the name twice ie. abc.xls turns into abc.xls_suffix.abcDo you know a solution?
-
Within Windows XP you need to use a double percentage mark to catch filesnames (in this case) into variables. Also you have to remove the brackets or the command after the DO statement will not work properly
The DOS command should look something like this:
for %%i in (*.*) do ren "%%i" "prefix_%%i"
-
I haven’t got a clue what all that is about I think you folks are making this way too hard on yourselves, I just use :
File1.jpg
file2.jpg
file3.jpgren file*.* f*.*
If I have done that right it will display all of the files with D1, d2 and d3.
However; I am running into problems with this where I am not able to rename it to exactly what I want, I can change the first letter but the second will remain.
I will continue to look at the formula you folks have given here. I of course appreciate your help, thanks
Corey Petersen
-
Unfortunately the above code fails for me. The existing filename is exactly 8 chars and the text I want to add as a prefix is 9 chars. Running the bat one time yields the following results:
* some files renamed prefix_original.ext
* some files renamed prefix_prefix_original.ext
* and some files renamed prefix_prefix_prefix_original.txt
Sigh.
-
Thanks - worked like a charm exactly as described (with 1 % symbol).
-
I’m trying to add a digit to my photos numbering to take them from thousands to tens of thousands. E.g. IMG_2450 to IMG_12450.
I did this about a year ago using Command Prompt, but I’m having trouble replicating those results today.
I tried: ren IMG_*.jpg IMG_1*.jpg and variations like: ren IMG_????.jpg IMG_1????.jpg
But what ends up happening is that instead of adding the number 1, command prompt ends up replacing the first character of the existing string of numbers.
So, IMG_2450 becomes IMG_1450 rather than IMG_12450.
What am I doing wrong here?
-
Thanks - you have a great site and it is accurate and the scrips work.
-
You cant remove multiple suffixes If you have file test.txt.log you can’t rename it to test.txt but in PowerSehll you can delete in all subdirectories Get-Childitem -recurse * -include *.txt.txt | Rename-Item -NewName $_.name -replace "txt.txt","txt"
-
for %i in (*.*) do (ren "%i" "%i_suffix")
or
for %i in (*.*) do (ren "%i" "prefix_%i")