Renaming Multiple Files At Once (Windows)

The Windows Explorer allows files to be renamed easily, by single clicking on the name and then editing it; however, this can be quite tedious if there are many files to be renamed. (Attempting to select a range of files and then using right-click + Rename doesn’t work. Only the last file in the range is renamed.)

Command Prompt to the rescue. This tip will show you how simple it is to use the REN command in the command prompt to do a wildcard rename.
REN, of course, is short for rename. The REN takes the form of “REN oldfilename newfilename” (or, you can spell out RENAME, if you prefer). Both the old filename and the new file name can contain wildcards.

Change Filename Extensions: Say, for example, you obtain a large set of ASCII files, all of which have names that end with a “.ASC” filename extension, but you need them to be named with “*.TXT” extensions instead. (In the following illustrations there are only two such files, but imagine that there are hundreds.)

DOS REN command

Quirky Wildcard Meanings: When wildcards appear in the old filename, they hold the usual wildcard meanings. A question mark matches exactly 1 character, while an asterisk matches any number of characters. The wildcard matching algorithm is a bit quirky when it comes to how the wildcards are treated in the new filename, though. This is a holdover from the ancient days of DOS when filenames were restricted to 8.3 file name (a maximum of eight characters for the name, and a maximum of three characters in the one-and-only filename extension). To make a long story short, the REN command only deals with whole parts when building the new filename (a part being either the filename to the left of the rightmost period, or the extension to the right of the rightmost period).

So, the following three commands are actually equivalent:

REN H*.asc H*.txt
REN H*.asc *.txt
REN H*.asc G*.txt

They all say, “Find any files that begin with H and end with .asc, and rename them so that they end with .txt”. In all three cases, when the rename command sees the asterisk in the name part of the new name, it assumes that any accompanying characters (H, G) are irrelevant and that what you really mean is for the new name to propagate whatever was found as the name part of the old name, and to just change the extension. Weird, huh?

Removing Filename Extensions: A common situation is that a file will have a name that ends with two filename extensions, often because some automated process added the second extension. (We showed one such process for converting Microsoft Word documents to plain ASCII text that resulted in file names such as “Huckleberry_Finn.doc.txt”. It is also quite common for backup copies of files to be saved by tacking on “.bak” to any existing filename extension, as in “Huckleberry_Finn.txt.bak”.) Again, imagine that there are hundreds of such files that need to be renamed so that they end in only the one filename extension.

There is a bit of a trick to removing such extraneous filename extensions with the REN command. You’d think that the logical thing to do would be to issue a command such as this:

REN *.bak *

Unfortunately, as we mentioned above, the two wild cards don’t really correspond. It is another quirk of the rename command to that when the filename pattern for the new filename does not contain any period, then it is assumed that the pattern refers to the entire filename with extension. Thus, a new filename pattern of just “*” will match the entire filename that was found. In other words, it renames the file from what it was to what it was (i.e. does nothing).

So, the trick is to go ahead and add the period, like this:

REN *.bak *.

Now, the asterisk in the new name only corresponds to the filename part of the old name. The “.bak” is dropped in favor of the nothing that is to the right of the period in the new filename pattern. Hopefully, I have not confused you by the fact that, in this case, “the filename part” refers to everything up to the rightmost period. So, in “Huckleberry_Finn.txt.bak”, the filename part is “Huckleberry_Finn.txt” and the extension part is “bak”.

Related articles:

Trackbacks & Pings

Comments

  1. Hmm, in WinXP Pro and Vista, highlighting a group of files and chooses File->Rename, or right-click->Rename or F2 all allow me to rename all of the files to a single name and Windows will append (copynum) to each file. Do I have some secret, fancy, special tool installed?

  2. mawes, XP, and I gather Vista, rename files through the GUI but as I recall it ’s rather limited. The REN fucntion allows the use of wildcards and extension changes amongst other possibilites I believe.

  3. Your timing is impeccable, thanks for the tip!

  4. Mawes: That’s interesting. I don’t see much use for that copynum behavior — well, maybe for obfuscating a set of filenames. I must not have actually tried having the GUI rename multiple files to a fixed name, and just assumed it would only rename the last file. (I probably got as far as not being able to enter a wildcard in the new name, gave up, and hit Cancel.) Thanks for the clarification.

  5. I can also already select a group of files and rightclick rename them, with a number appended to the end. I have Windows XP, maybe it doesn’t work in windows home.

  6. This was very help ful for me

    Thanks

  7. what if you have characters in the beginning of a filename that you would like to del? I’m using this app to convert some files and it spits out [extra]-filename.exe and what I’m hoping to do is take out the “[extra]-” out of the file… it seems that whenever I use the ren command to do this it does not del the first char, but simply renames them….
    ex c:\ren [extra]-*.exe *.exe
    will obviously not change anything but if you change the char it will overlap
    ex c:\ren [extra]-*.exe 123*.exe
    will change the files to 123tra]-filename.exe
    thanks for your time…

  8. You can use ? to get around the wildcard limitation, by running renames with every number of ? that might be relevant.

    Eg to get rid of the [extra], you’d run:

    rename [extra]?.exe ?.exe
    rename [extra]??.exe ??.exe
    rename [extra]???.exe ???.exe

    up to the maximum number of characters that you expect will be used.

    Ugly, definitely, but better than doing it manually. This is why the Linux command line supports regular expressions.

  9. i got a situation in my work ,where i have to change certain word in the file name to some other word…what i mean is

    i have 50 files(.html) ,all these files have a common word in thier names(file names) which has to be replaced wth some other word in all the file names….

    What i am doing now is ..going to each file ->pressing F2 and changing that word.

    Is there any way in command prompt or some other way such that i can replace that word at a time in all the filenames by giving path of that folder(containg those html files)

    Please help me out as this is very tedious and boring activity …time consuming

  10. I have been searching the whole internet, trying to find out how to rename file extensions in a batch fashion, drilling down through all subdirectories. I can’t find anything. Is this command really limited to just the directory you’re in?

    For instance, I want to rename all .html files to .php files, but throughout the whole folder, including all it’s subdirectories.

    Brief Example:

    /mysite/index.html
    /mysite/home/index.html
    /mysite/about/index.html
    /mysite/contact/index.html

    So, I know that I can go into the /mysite/ folder and do the following:

    rename *.html *.php

    However, obviously, it only renames that index.html file and does not take care of /mysite/home/index.html, etc.

    I have tried things like, backing out one level and doing the following:

    rename /s *.html *.php

    But that does not work. Again, I have searched all over the web with using keywords like:

    windows rename cmd wildcards file extensions, etc.

    Can someone help me with this? It would be great to have an article on this too.

  11. It appears that the /S flag *should* work. Maybe see if this thread will help you: http://www.daniweb.com/forums/thread80448.html#

  12. Thank you for that link. I did try this:

    for /f “usebackq tokens=*” %h in (`dir /b /A:-D`) do @ren *.extn1 *.extn2 >nul || echo Failure: %h -^> .tql && echo Success: %h -^> .tql) && for /f “usebackq tokens=*” %g in (`dir /b /s /A:D`) do (for /f “usebackq tokens=*” %h in (`dir /b /s /A:-D`) do @ren *.extn1 *.extn2 >nul || echo Failure: %h -^> .tql && echo Success: %h -^> .tql)

    I renamed *.extn1 to *.html
    I renamed *.extn2 to *.php
    I renamed .tql to .html

    I don’t know if I totally understood the thread or not though and when I tried to use it, it just kept looping through all of the folders but not actually renaming.

    I am doing research on the batch and how to do that, but I think I will end up with the same problem.

    Could you rewrite their example to my example so that I understand how it is supposed to be written?

    Thanks for your help!

  13. suppose my file name is pramodxxx.txt. I want to rename it to xxx.txt. how do i do this in command prompt. rename command

  14. there are many files starts with fixed name pramodxxx.txt, pramodxxxx.txt. how can I rename those to xxx.txt , xxxx.txt , through the ren command.

  15. Any responses for #comment-23781 ?

    Could you rewrite their example to my example so that I understand how it is supposed to be written?

  16. Is there too little activity on this web-site? No response to #comment-23781 on June 4th??

  17. Another complicated renaming problem: I want to rename multiple files in subdirectories. Furthermore for the renaming, I have a text file where column 1 lists the old names and column 2 lists in order the new names I want to give the file. Is there a way to say, “Look in all these subdirectories. When you find file ‘X’ rename it ‘1′, file ‘Y’ rename it ‘wr’, file ‘tz’ rename it ‘1592′, etc. based on the columns in the text file.”

  18. You can rename files in nested directories using;

    for /r %x in (*html) do rename “%x” *.php

    (Renames all html files php down the whole directory tree).

    AFAIK it is impossible to rename foo*.bar to *.bar using vanilla window “rename” command. (The method Carl describes won’t work).

  19. …but come to think of it, you can use a bat file;

    for %%i in (”FOO_name*.html”) do (set fname=%%i) & call :rename
    goto :eof
    :rename
    ::Cuts off 1st 4 characters of fname, then appends prefix
    ren “%fname%” “my%fname:~4%”
    goto :eof

    Given a list FOO_name_1.html, FOO_name_2.html this output myname_1.html, myname_2.html

  20. I am having the problem with the asterisk as well. I want to rename a bunch of dog files from shepherd to basset.

    When I use

    ren shepherd*.* basset*.*

    I get unexpected results… the files end up being renamed to bassetrd… (the “rd” remains in the filename). It makes logical sense, but it isn’t the result I want.

  21. simple yet powerful, thanks

  22. i have the following problem:
    i’d like to rename (dos command) the 500p.D2011049.txt to D2011049.txt. do you have any idea ?
    thank’s

  23. check this post………

    http://www.nota-bene.org/+DOS-Shell-script-to-rename-multiple-files-in-one-line+

  24. That’s awesome! - I’ve been dumping out filelists and making batch files for ages.

    yay for DOS

  25. i want to rename all *.prn files to *.txt files in all subfolder by dos command.

    any idea about that so please send mail to me..

    thanking you

    soyeb

  26. for those with *.*.* in the filename, this worked for me:

    open command prompt
    navigate to folder with files
    ren *.* *.*.txt

  27. EDIT

    ren *.*.* *.*.txt

  28. Hey I hav a peculiar case of all my files beginning with a special character .. i.e. names are like (H1 2011*).doc .. i.e. all files have an open bracket ( as the first character of name and closed bracket ) as last character before extension I have to remove all brackets from all names
    … I tried
    ren (*.* *.* but this hasn’t worked .. please suggest what can I do

    I’m running Windows 7

    ~thx

  29. I am also running windows 7. Worked perfectly. Thanks

  30. The little trick at the end worked great! Thanks.

  31. Actually the rules for wildcards in the REN target name are not a legacy from the 8.3 standard, though they may have been formulated to be compatible. They are more complicated (and useful) than anyone knew :-)

    Check out this StackExchange question and answer for more info:

    http://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards

  32. The trick at the end was the key! ren *.bak *. got me to *.txt woohoo!

  33. We can add one more trick and use the command to recursively rename files in the subfolders too. I’ve written scripts for the same. Please check it here
    http://www.windows-commandline.com/rename-file-extensions-bulk/

  34. i just want delete a part of my fileNames.
    i’m search in windows 7 in folder (for example) *_fanart.jpg*
    matched (example): Adams Family_fanart.jpg
    Die Hard_fanart.jpg
    Batman_fanart.jpg …
    …etc
    now i wanna remove (or rename) the “_fanart” from all this marked matched files.
    how can i do this in one time , not one by one?
    (sorry for my bad english)

  35. @Kid
    I don’t know what version of windows you’re using but you could try PowerShell. PowerShell is available from XP on and comes installed with Windows 8. Once installed you can try the following:

    Get-ChildItem *.jpg | Rename-Item -newname { $_.name -replace ‘_fanart’,'’ }

  36. I’m looking for command prompt commands that will take all the files in a folder and replace with the names in a list from a text file.
    This isn’t a simple find/replace, replace all, delete a common word (like you’d see when renaming a bunch of pictures from a camera).

    I’m trying to rename a large (>200) list of files that I’m getting off of a scanner with a crazy file name. For each of these, I’ll have a list (as a text file), in order, that will correspond.

    For a simplified example, if my file names are a.pdf; b.pdf, c.pdf….z.pdf, and I would like to rename these from my text file to dog.pdf, cat.pdf, fish.pdf….goat.pdf.

Post a Comment


Your email is never published nor shared. Required fields are marked *



© 2006-2007 Maxim Software Corp.  All rights reserved.