This post talks about how to batch rename file extensions from command line. No 3rd party tools are required. At the end of the post, you can find a download link for the batch file script.
Batch rename file extensions in bulk from CMD
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 batch 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 photo@file"
Similarly, we can add a number to a file name.
forfiles /S /M *.jpg /C "cmd /c rename @file 99@file"
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\""