≡ Menu

Rename file extensions in bulk, including files in subfolders

This post talks about how to rename file extensions in bulk using simple commands. No 3rd party tools are required. At the end of the post, you can find a download link for the batch file script.

Change file extensions in bulk:

We can change the extension of files in batch using rename command. For example, let’s say you have set of files with extension .log and you want to rename them to .txt.  You can do this with the below command

rename  *.log   *.txt

The drawback with this command is, we can’t use it to rename file extensions recursively in the sub folders. Next, we’ll look at a solution which allows us to do so.

Recursively rename file extensions

If you want to rename files from one extension to another, recursively in all sub folders, then you can use the below command.

forfiles /S /M *.ext1 /C "cmd /c rename @file @fname.ext2"

For example, if you want to rename all xml files to txt files, the command would be as below

forfiles /S /M *.xml /C "cmd /c rename @file @fname.txt"

Remove file extensions in batch

The below command would remove extension for the specified file types.

forfiles /S /M *.ext /C "cmd /c rename @file @fname"

If you want a batch file script, it can be downloaded from the below link:
Batch file script for renaming file extensions in bulk

Add prefix to file names in batch

If you want to add any prefix to file names, it can be done as in the below example. Here we try to add ‘photo’ to every jpg file in the current folder and subfolders.

forfiles /S /M *.jpg /C "cmd /c rename @file [email protected]"

Similarly, we can add a number to a file name.

forfiles /S /M *.jpg /C "cmd /c rename @file [email protected]"

Handling names with white spaces

If the new name you want to assign to the files has white space within it, it can be done by adding double quotes around the file name. So that forfiles does not misinterpret this doublequotes, you need to escape them with ‘\’
For example to add ” – pic.jpg” to each of the jpg files, the command would be as below.

forfiles /M *.jpg /C "cmd /c rename @file \"@fname - pic.jpg\""
{ 45 comments… add one }
  • devilJake June 18, 2014, 4:08 pm

    I want only the extensions of files starting with certain type of names to be renamed, within the folder as well as subfolders.
    How can I do it?
    For example:
    rename all files starting with the name “*-common.xml” to “*-common.xml.txt”

    The other .xml files have to be left intact.

    • admin January 14, 2015, 8:18 pm

      This should do

      ren  *-common.xml  *-common.xml.txt
  • devilJake June 18, 2014, 5:08 pm

    Figured it by out myself.

    forfiles /S /M *-common.xml /C “cmd ren *-common.xml *-common.xml.txt”

    • admin January 14, 2015, 8:20 pm

      If you are using forfiles, you donot need to use * again in the cmd part.

      forfiles /S /M *-common.xml /C "cmd ren @file @file.txt"
  • Cathryne July 10, 2014, 3:02 pm

    Thanks for these hints :-) Is it also possible to include a space in the prefix? I tried the following:

    forfiles /S /M *.jpg /C "cmd /c rename @file "photo location - "@file"

    => The syntax of the command is incorrect

    forfiles /S /M *.jpg /C "cmd /c rename @file 'photo location - [email protected]"

    => ERROR: Invalid argument/option…

    • admin January 9, 2015, 5:46 am

      The file name need to be surrounded with double quotes if it has white spaces. Added example command to the post.

  • Volker September 25, 2014, 9:13 am

    I have here some files with ending like this
    (1).txt
    e.g. file_a(1).txt
    I tried this
    forfiles /S /M *.ext1 /C "cmd /c rename @file @fname.ext2" for renaming them to .txt
    e.g. file_a.txt

    Can you help me?

  • Will October 23, 2014, 5:06 pm

    The “Add prefix to file names” code is faulty. When I ran it, it keeps adding the prefix to all of the files in the folder over and over again until I kill it.

    • admin October 24, 2014, 12:02 am

      It worked fine for me. Can you tell me how many files you have in the folder that you were intending to add the prefix?

    • David August 24, 2016, 6:04 pm

      Same issue here. I believe it happens when there are special characters in the file name (i have brakets “[]” and commas “,”.

  • Masim October 29, 2014, 6:57 am

    I just tried to rename midi karaoke files to midi files,

    forfiles /S /M *.kar /C "cmd /c rename @file @fname.mid"

    This doesn’t work if the file name has spaces in it, (OS Win XP Sp3).
    But I found other solution that works flawlessly

    FOR /R %f IN (*.kar) DO REN "%f" *.mid
    • Abdulqader Zeniddeen December 13, 2015, 8:55 pm

      Thanx a million times man! you saved me

  • Ap.Muthu January 8, 2015, 6:32 am

    None of the websites including this has alluded to the renaming of files by removal a second extension.

    If a folder contains several files like *.mo.po and *.mo as file extensions, how do we selectively rename the *.mo.po to *.po whilst not touching the *.mo that were already there?

    • admin January 9, 2015, 12:18 am

      Try this command

      forfiles /M *.mo.po  /C  "cmd /c rename @file @fname"

      It works only if you don’t have files with the same name and two different extensions.
      ex: 1.mo.po and 1.mo. You would get error in renaming as a file already exists with the name.

      Let us know if it works for your usecase.

  • steve szajko January 14, 2015, 7:44 pm

    A virus added .zwrkocj to all file types, how would I remove that?

    • admin January 14, 2015, 8:15 pm

      If it has converted files like file.ext to file.ext.zwrkocj then I think you can run the below to get back to original state.

      ren *.zwrkocj *. 
  • cta January 23, 2015, 5:34 pm

    How to append the name of the parent folder onto the prefix of the file name?

  • garegin March 12, 2015, 7:52 pm

    forfiles /S /M *.ext /C “cmd /c rename @file @fname”

    changed files in all of C drive, right?
    how can you specify a specific folder and its subfolders?
    how can you turn off recursion?

    • admin March 14, 2015, 8:24 pm

      We can’t restrict the recursion to one level. You might need to write a custom batch script to do that.

  • Ankur April 4, 2015, 9:09 pm

    Hi

    I have one question.
    I created a bat file for renaming all the files to one extension

    forfiles /S /M *.* /C "cmd /c rename @file @fname.jpeg"

    but this also changes the extension of my bat file.
    Can I exclude bat file in any way.
    Thanks for your help on this.

    • admin April 6, 2015, 4:59 pm

      Why not place this bat file in some other folder, outside the current directory tree?

  • Michael April 10, 2015, 8:40 pm

    Great stuff guys. This probably saved me about an hours worth of work.

  • Austin April 28, 2015, 9:38 pm

    I can’t seem to get the white spaces code to work.
    forfiles /s /m *.* /C “cmd /c rename @file \”(PA) @file\””

    Your Example:

    forfiles /M *.jpg /C "cmd /c rename @file \"@fname - pic.jpg\""

    Is there anything that is wrong with the above code. This is the following error “Unrecognized token in source text.”

    • admin April 28, 2015, 10:33 pm

      It worked for me.

      c:\tmp>dir /s /b
      c:\tmp\testfile.txt
      c:\tmp>forfiles /S /M *.txt  /C "cmd /c rename @file \"(PA) @file\""
      c:\tmp>dir /s /b
      c:\tmp\(PA) testfile.txt
      

      I guess something could be wrong if you had copy pasted the command. Especially the double quotes usually get wrong. I would suggest to type the command(rather than copy/paste), it should work.

    • Austin May 4, 2015, 5:17 pm

      Would the error be caused by running the commands simply from a PS1 Script? That’s the only reason I can think of.

      Well I ended up Scripting a simply tool in Access VBA.

  • Vishwa June 10, 2015, 1:02 am

    ‘forfiles’ command seems to have come very close to serving my need but is there a way to remove a prefix from a set of files recursively?

    Example:
    TT-*.* to *.*
    I want to remove ‘TT-‘ from all the files

  • Jeff August 5, 2015, 5:28 pm

    Hello,

    Any idea’s on how to rename hidden files?
    When I run the command it can’t find the files because they have the hidden attribute.
    I don’t see a /H or similar switch in the command help.

    Any help would be appreciated.
    Thank you.

    • admin August 6, 2015, 7:40 pm

      You can remove the hidden attribute first with ‘attrib -h’ and then rename the file.

  • Niyaz August 10, 2015, 7:31 am

    Hi,

    i want to add one set of numbers to file names and have multiple folders containing few files in every forlder
    like folder 1003 has 3 files with names like : 1003_blue_M , 1003_Black_M, 1003_Red_S, etc… now i want to change the file name to 1003_blue_M_12345, 1003_Black_M_12345, like wise…. every folder will have new number to be added..kindly suggest how i can replace the entire bulk of files ->in folders
    Thanks
    Niyaz

  • R D Naik August 28, 2015, 12:42 pm

    I do not understand the example for changing file extension of particular file and where to apply this command. pl. clarify

  • Chriss September 9, 2015, 4:30 pm

    as Will said:
    “The “Add prefix to file names” code is faulty. When I ran it, it keeps adding the prefix to all of the files in the folder over and over again until I kill it.”

    It keeps adding the prefix till you kill it.
    is there a stop or pause code ?

    • admin September 10, 2015, 2:46 am

      I am not able to replicate this. It is stopping after renaming all the files.
      May be you guys have a different setup in your cases.

  • Ghetto28 September 21, 2015, 9:55 am

    Hi,

    How to remove double quotation using forfiles to save as text file. Please see below my code.

    forfiles /D +0 /P C:\KHAS\BackUp_and_Recovery\db_BackUp\Full\FO\ /M *.BAK /C "CMD /C ECHO @FNAME, @FDATE, @ftime, 20001>C:\KHAS\BackUp_and_Recovery\Backup_Files\@FNAME"

    the result was this : “FO_FullDBBackup_201509211126”, 9/21/2015, 11:26:50 AM, 20001

    i want to look like this : FO_FullDBBackup_201509211126, 9/21/2015, 11:26:50 AM, 20001

    than you,

  • john October 13, 2015, 2:53 pm

    hi, I wanted to know how to rename files so that if it detects the same name it will change the name to add a number to it for example:

    ren *.* *.jpg
    so if it has 1.thm 1.thumb it will rename the first file 1.thm and then when it tries to rename 1.thumb to jpg it says that its duplicate. so I need it to rename something like 1-1.jpg or 1a.jpg, 1b.jpg, etc.

    thanks in advance.

  • erslog October 25, 2015, 4:25 pm

    Hi I tried to append the prefix XTO_12345678W500_ to about 200 pictures

    the command will repeat the addition (XTO_12345678W500_) until the file name is 256 almost characters! Then crashes.
    I tried to add only 12345678W500_ and it works fine.
    To this tried to add the remaining XTO_ and it did it again: fill the name to about 256 (did not count!) characters!!!! and then crashes!

  • Ben December 9, 2015, 8:37 pm

    Hello

    I want to change an extension from .exe to .pdf or .jpg without changing the proprietes of .exe
    Can somebody help me with this?

    Thank you

  • ritz December 22, 2015, 2:49 pm

    forfiles /s /M *.txt /C “cmd /c rename @file [email protected]

    when i am using the above command for more then 40 files in the folder it gives an error with the repetition of filename with .ext ?
    any solution guys with some minor changes in the above code so i can rename the 100s of filename without repeating it.

  • dips January 24, 2016, 12:07 am

    Hi

    I want to remove prefixes from file names. I have successfully used the following to rename files in a directory, but it does not work for subdirectories
    rename “01 – *.mp3” “/////*.mp3”

    I tried
    forfiles /S /M 0*.mp3 /C “cmd /c rename @file \”[email protected]\””

    however it gives me an error that
    ERROR: Files of type “0*.mp3” not found.

    however dir 0*.mp3 /s works fine, so there are files matching the criteria

    Variations to enclose the search in “” also give the same error. I tried
    forfiles /S /M “0*.mp3” /C “cmd /c rename @file \”[email protected]\””

  • Aaron February 10, 2016, 4:15 pm

    I have a script that renames files and adds a time stamp for the current time. the script works great however it will only execute on one file in a folder each time it runs. I need it to run on all file in a folder each time. What am i Missing?
    @echo off
    for %%f in (*.*) do echo %%f
    for /f “delims=” %%a in (‘wmic OS Get localdatetime ^| find “.”‘) do set dt=%%a
    set YYYY=%dt:~0,4%
    set MM=%dt:~4,2%
    set DD=%dt:~6,2%
    set HH=%dt:~8,2%
    set Min=%dt:~10,2%
    set Sec=%dt:~12,2%
    set stamp=D%YYYY%%MM%%DD%T%HH%%Min%%Sec%

  • SM February 16, 2016, 1:42 pm

    Hi,
    I want to use the rename command for changing the extension of files in a folder to “*.gz.par”.
    For eg the files in the folder are:
    abc.par
    xyz.gz.par
    I need to rename the abc.par to abc.gz.par and xyz.gz.par should stay the same.
    Now when I run the command: rename *.par *.gz.par
    it changes the abc.par correctly to abc.gz.par
    but it also changes xyz.gz.par to xyz.gz.gz.par
    Please help…

  • nabil February 18, 2016, 2:22 pm

    Hi,
    every morning at work i start my day by serching a file received the night before on folder1 and having name like RetxxxxxxxDDYY (the DDYY is the date of the last working day when the file has been created).

    Is there a way to copy the last file received from the folder1 to folder2 using a script knowing that the files created avery day still on this folder.
    Many thanx in advence

  • Masza February 25, 2016, 9:01 am

    How do I change files’ type in D drive?

  • spinnaker March 29, 2016, 4:01 pm

    Kryptik virus or trojan changed the file names from original file names to include .crypted – I used the remove file extensions in batch command copied the original line into notepad modified it to be
    forfiles /S /M *.crypted /C “cmd /c rename @file @fname”
    then pasted it from notepad into the cmd window
    And voila all the files that had been changed got changed back to the original filenames. Thank you for this resource.
    Thank you thank you.

  • deekay May 7, 2016, 7:54 am

    A virus added .crypted to all file types, how would I remove that? like .doc.crypted , .ppt.crypted, jprg.cypted

    i have done ren*.* *. but i was for one directory, i want forfolder and sub folder or hole directory in one batch file

  • frank lu May 28, 2016, 4:45 pm

    Thank you Masim!

    Your command line really works, helped me remove all “.cryped”

Leave a Comment