How to Rename All Files with a Specific Extension in Batch Script
Bulk-renaming files is a frequent and time-saving automation task. A common scenario is needing to change the extension for all files of a certain type in a directory, for example, converting a batch of .txt files to .log files or .htm files to .html. Windows Batch provides two primary methods to accomplish this: a simple one-liner using the REN command with wildcards, and a more powerful approach using a FOR loop.
This guide will teach you both methods, explain when to use each one, and show you how to extend the logic to handle subdirectories and more complex renaming patterns.
The Core Command: REN (RENAME)
The REN (or RENAME) command is the simplest tool for this job. It supports wildcards (* and ?), allowing it to operate on groups of files simultaneously.
The syntax for a bulk extension change is straightforward:
REN *.old_extension *.new_extension
- The first
*is a wildcard that matches any filename. - This command tells the processor to find all files ending in
.old_extensionand rename them, keeping the original filename but applying.new_extension.
Basic Example: A Simple One-Line Rename
Let's change all .txt files in the current directory to .log files.
Suppose you have this list of files in the directory:
- report-2023-10-25.txt
- server-status.txt
- user-list.txt
- important-data.csv
This single command is all you need for your script:
@ECHO OFF
REM Renames all .txt files to .log files in the current directory.
REN *.txt *.log
After running the script, the file extensions are changed. The output is the following:
- report-2023-10-25.log
- server-status.log
- user-list.log
- important-data.csv
The .csv file was correctly left untouched. This method is fast, efficient, and perfect for simple, top-level directory changes.
The FOR Loop Method for More Control
While the REN command is easy, it's not very flexible. It can only change the extension. If you need more control,for instance, to add a prefix or modify the filename itself, you must use a FOR loop.
A FOR loop iterates through a set of files and executes a command for each one individually. This allows us to use parameter expansions to separate a file's name from its extension.
This script performs the same task as the REN command but does so file by file, giving us more control.
@ECHO OFF
REM Use a FOR loop to rename all .txt files to .log.
ECHO Starting rename process...
FOR %%F IN (*.txt) DO (
REN "%%F" "%%~nF.log"
)
ECHO Done.
How it works:
FOR %%F IN (*.txt) DO ...: This starts a loop that will find every.txtfile. For each file it finds, it stores the full filename in the%%Fvariable.REN "%%F" ...: This calls theRENcommand for the current file (e.g.,"report-2023-10-25.txt")."%%~nF.log": This is the key. The~nmodifier extracts only the name part of the file from the%%Fvariable. We then manually append the new.logextension.
While more verbose for a simple extension swap, this method is the foundation for more advanced renaming operations.
Common Pitfalls and How to Solve Them
Problem: Renaming Files in Subdirectories (Recursive Rename)
The REN *.txt *.log command is not recursive. It will only affect files in the current directory and will ignore any subfolders.
Let's see the error:
If your directory structure is C:\Logs\ and C:\Logs\Archive\, REN *.log *.txt run from C:\Logs will not touch the files inside the Archive folder.
Solution: Use a FOR /R Loop
The FOR command has a recursive switch, /R, that walks an entire directory tree. This is the correct and only built-in way to perform a recursive rename.
@ECHO OFF
SET "START_FOLDER=C:\Logs"
ECHO Recursively renaming *.log to *.txt in %START_FOLDER%...
FOR /R "%START_FOLDER%" %%F IN (*.log) DO (
REN "%%F" "%%~nF.txt"
)
ECHO Recursive rename complete.
This script will start at C:\Logs and go into every subdirectory, renaming any .log file it finds.
Problem: Handling Complex Filenames or Paths with Spaces
If a filename contains special characters or spaces, not quoting the variables can cause the REN command to fail.
Solution: Always Quote Your Variables
The FOR loop method provides a natural way to handle this. By enclosing the variables in double quotes ("%%F" and "%%~nF.log"), you ensure that the REN command receives the full, correct filename, even if it contains spaces.
REM This is the safe way to structure the command inside a FOR loop.
REN "%%F" "%%~nF.log"
This is a key reason why the FOR loop is considered more robust for scripting than the simple wildcard REN.
Practical Example: Adding a Prefix While Renaming
This script demonstrates the power of the FOR loop. It finds all .jpeg files and renames them to .jpg, while also adding the prefix IMAGE_ to the filename.
For example, consider these files in the directory:
- vacation_photo.jpeg
- cat_picture.jpeg
and the script:
@ECHO OFF
ECHO Adding prefix and changing extension for all .jpeg files...
FOR %%F IN (*.jpeg) DO (
REN "%%F" "IMAGE_%%~nF.jpg"
)
ECHO Process complete.
Output:
- IMAGE_vacation_photo.jpg
- IMAGE_cat_picture.jpg
This kind of complex rename is impossible with the simple REN *.jpeg *.jpg command.
Conclusion
Both methods for renaming files by extension are useful, and choosing the right one depends on your needs.
- For a quick, simple change in a single directory, the
REN *.old *.newcommand is the fastest and easiest solution. - For more complex operations, such as renaming files recursively through subdirectories or modifying the filename in addition to the extension, the
FORloop (especiallyFOR /R) is the more powerful and robust choice.
For any serious scripting, adopting the FOR loop method is a best practice, as it provides greater flexibility and safely handles filenames with spaces.