How to Rename Part of Filename in Batch Script

  1. Understanding Batch Script Basics
  2. Using Batch Script to Rename Filenames
  3. Advanced Renaming Techniques
  4. Conclusion
  5. FAQ
How to Rename Part of Filename in Batch Script

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

  1. 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.

  2. 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.

  1. Is it possible to rename files in subdirectories?
    Yes, you can modify the script to include subdirectories by using the /R switch in the for command.

  2. What if I want to preview the changes before renaming?
    You can add an echo command before the ren command to display the proposed changes without actually renaming the files.

  3. 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.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
MD Aminul Islam avatar MD Aminul Islam avatar

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

Related Article - Batch File