How to Rename a File in Batch Script

MD Aminul Islam Feb 02, 2024
How to Rename a File in Batch Script

Sometimes, we need to rename a file for various purposes of the system, and we need to do it programmatically. If you are looking for a solution to rename a file or a set of files using a Batch script, then you may follow this article.

In this article, we will see how we can rename a file or a set of files by using a Batch script. We are going to discuss two situations here.

The first one is working with a single file, and the second one is working with multiple files. Also, we will see necessary examples and explanations to make the topic easier.

Rename a File in Batch Script

In the Batch script, a command named REN or RENAME is used to rename a file or a set of files. The keyword REN is the sorted version of RENAME.

The general format to rename a single file with this command is:

RENAME [DRIVER:][YourFilePath][DirectoryName1 | FileName1] [DirectoryName2 | FileName2

Let’s see some examples with this command. In the below example, we will rename a file named Test.txt to Rename.txt.

The code for our example will be,

RENAME "Test.txt" "Rename.txt"

In the example above, we have renamed a single file. Now, we will discuss how we can rename multiple files with just one Batch script.

In this case, you have to understand that the multiple file renaming mainly works on a file type basis. For example, if you have some file with the extension .txt, and want to modify all the file extensions to .bat, then you may follow the code below.

RENAME *.txt *.bat

The above example will change all the files with an extension of .txt to .bat in a specific directory. Here, the * means all the files inside the directory.

Remember you have to run this command inside the directory where your targeted files are. If your script is out of that directory, you can specify the directory; then, you need to specify the directory with the command like the one below,

RENAME "G:\MyDir\simple.txt" Test.txt

The above code will rename the file with location G:\MyDir\simple.txt to Test.txt. Remember, you have to provide the exact location on the command, not the directory location.

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 Rename