> Thoughts > DOS Shell script to rename multiple files in one line

DOS Shell script to rename multiple files in one line

About this article

An article by Stéphane

Published on 15 March 2007

Short URL:


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.


    • 13 December 2007

    that was useful, i needed to suffix something and this almost worked
    for %i in (*.*) do (ren "%i" "")
    problem with this is that the file extension gets included in the name twice ie. abc.xls turns into

    Do you know a solution?

    Reply to Jimmy

    • 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

  • 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 :


    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

    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


    Reply to Jim

    • 18 January 2009

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

    Reply to anonymous

    • 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

    • 8 January 2016

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

    Reply to anonymous

    • 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 $ -replace "txt.txt","txt"

    Reply to Giors Geks

    • 20 July 2016

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


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

    Reply to Jammy dodger


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

Who are you?
Your post
  • To create paragraphs, simply leave blank lines. All outgoing links will feature an attribute rel="nofollow". Please do not spam.

This is the Desktop version Show the Mobile version