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

  • Jimmy (13 December 2007)

    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.abc

    Do you know a solution?

    Reply to Jimmy

  • anonymous (12 March 2008, in reply to Jimmy)

    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"

    Reply to anonymous

  • anonymous (14 September 2008, in reply to anonymous)

    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.jpg

    ren 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

    Reply to anonymous

  • Jim (13 December 2008, in reply to anonymous)

    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.

    Reply to Jim

  • anonymous (18 January 2009)

    Thanks - worked like a charm exactly as described (with 1 % symbol).

    Reply to anonymous

  • Michael (20 May 2013)

    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?

    Reply to Michael

  • anonymous (8 January 2016)

    Thanks - you have a great site and it is accurate and the scrips work.

    Reply to anonymous

  • Giors Geks (14 March 2016)

    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"

    Reply to Giors Geks

  • Jammy dodger (20 July 2016)

    for %i in (*.*) do (ren "%i" "%i_suffix")

    or

    for %i in (*.*) do (ren "%i" "prefix_%i")

    Reply to Jammy dodger

pre-moderation

This forum is moderated before publication: your contribution will only appear after being validated by an administrator.

Who are you?
Your post

This form accepts SPIP shortcuts [->url] {{bold}} {italic} <quote> <code> and the HTML code <q> <del> <ins>. To create paragraphs, simply leave blank lines.

Hypertext link

(If your message refers to an article published on the web or to a page providing further information, please enter the title of the page and its URL below).