How to Rename Part of Filename in Batch Script

MD Aminul Islam Feb 02, 2024
How to Rename Part of Filename in Batch Script

Sometimes we need to rename a series of files with a specific sequence. Most of the project file contains this sequence, and we can easily do this in the Batch script by using a simple single-line command.

This short article will show us how we can change a specific part of the filename using the Batch Script. Also, we will discuss the topic by following some examples and explanations to make the topic easier.

Change a Specific Part of the Filename of Multiple Files in Batch

For this purpose, we will use a built-in Batch command, the REN or RENAME. This command mainly uses for renaming a filename, but we also can use this to change the filename of multiple files.

In our example below, we will see how to change a part of the filename on multiple files in Batch. To do this, let’s consider having the files below in a folder.

Files:

1_Test.txt
2_Test.txt
3_Test.txt
4_Test.txt
5_Test.txt

Now, we need to change the part of the filename Test with the ChangePart, but all other parts of the filename will remain the same.

Command:

REN *Test* ??ChangePart.txt

You can notice that we used ?? in our command. This will tell the system that the first two characters will remain the same, and the other part will be changed.

If you want the first three characters to remain the same, you must put ???.

After executing the above command, you will see that the filenames are changed.

Output:

1_ChangePart.txt
2_ChangePart.txt
3_ChangePart.txt
4_ChangePart.txt
5_ChangePart.txt

Take note that you have to run the command where the files are. So if you are not in that location, go to that location using the CD command.

The code example in this article is written in Batch and only for the Windows CMD.

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