How to Rename Part of Filename in Batch Script
- Understanding Batch Script Basics
- Using Batch Script to Rename Filenames
- Advanced Renaming Techniques
- Conclusion
- FAQ

Renaming files in bulk can be a daunting task, especially when you have numerous files that require similar modifications. Luckily, Batch Script offers a straightforward way to rename parts of filenames efficiently. Whether you need to change a prefix, suffix, or even a specific string within the filenames, mastering this technique can save you considerable time.
In this tutorial, we will walk you through the steps to rename parts of filenames using Batch Script. By the end, you’ll have the skills to manage your files with ease, making it a breeze to organize your projects or repositories.
Understanding Batch Script Basics
Before diving into the specifics of renaming files, it’s essential to grasp the basics of Batch Script. Batch scripting is a way to automate tasks in Windows by writing a series of commands in a text file with a .bat extension. When executed, this file runs the commands sequentially, allowing you to perform repetitive tasks efficiently.
To rename parts of filenames, we’ll utilize the for
loop and the ren
command. This combination allows us to iterate through files in a directory and rename them according to specified criteria.
Using Batch Script to Rename Filenames
Here’s a simple method to rename parts of filenames using Batch Script. This example demonstrates how to change a specific string in the filenames of all files within a directory.
@echo off
setlocal enabledelayedexpansion
set "oldString=old"
set "newString=new"
for %%f in (*%oldString%*) do (
set "filename=%%~nf"
set "extension=%%~xf"
set "newname=!filename:%oldString%=%newString%!!extension!"
ren "%%f" "!newname!"
)
endlocal
The above script starts by defining the strings you want to replace. The for
command iterates through each file containing the oldString
. Inside the loop, the script captures the current filename and its extension. It then constructs a new filename by replacing the oldString
with the newString
. Finally, the ren
command renames the file.
Output:
File1_old.txt renamed to File1_new.txt
File2_old.txt renamed to File2_new.txt
This method is particularly useful when you need to change a common part of several filenames. The enabledelayedexpansion
allows us to modify variables within the loop, ensuring that the new filename is constructed correctly before renaming.
Advanced Renaming Techniques
If you have more complex renaming needs, such as changing prefixes or suffixes, you can modify the script accordingly. Here’s an example that demonstrates how to change a file prefix:
@echo off
setlocal enabledelayedexpansion
set "oldPrefix=old_"
set "newPrefix=new_"
for %%f in (%oldPrefix%*) do (
set "filename=%%~nf"
set "extension=%%~xf"
set "newname=!filename:%oldPrefix%=%newPrefix%!!extension!"
ren "%%f" "!newname!"
)
endlocal
In this script, we define oldPrefix
and newPrefix
. The for
loop targets files that start with the specified prefix. Similar to the previous example, it constructs a new filename and renames the file accordingly.
Output:
old_File1.txt renamed to new_File1.txt
old_File2.txt renamed to new_File2.txt
This technique can be particularly beneficial for organizing files that follow a specific naming convention. By changing prefixes or suffixes, you can quickly categorize your files, making them easier to locate and manage.
Conclusion
Renaming parts of filenames in Batch Script is not only efficient but also a powerful way to manage your files. By utilizing simple commands and loops, you can automate the renaming process, saving you time and effort. Whether you need to change a specific string, prefix, or suffix, Batch Script provides the flexibility you need. With these techniques in your toolkit, you can keep your files organized and easily accessible.
FAQ
-
How do I run a Batch Script?
You can run a Batch Script by double-clicking the .bat file or executing it from the command prompt. -
Can I use Batch Script on other operating systems?
Batch Script is specific to Windows. For other operating systems, you would use shell scripts or other scripting languages.
-
Is it possible to rename files in subdirectories?
Yes, you can modify the script to include subdirectories by using the/R
switch in thefor
command. -
What if I want to preview the changes before renaming?
You can add anecho
command before theren
command to display the proposed changes without actually renaming the files. -
Are there any risks in using Batch Scripts?
If not used carefully, Batch Scripts can overwrite files or rename them incorrectly. Always back up your files before running a script.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn