The following article will use the “rename” or “ren” command to rename the file using a command prompt. Also, we will try to show some trick playing with the rename command. Just in case if you are looking to do this in multiple files check the following article – “rename multiple files in folder”.
Batch script rename file using command prompt
RENAME [drive:][path][directoryname1 | filename1] [directoryname2 | filename2]
Let’s see it in some examples of renaming the file.
How to rename file using command line:
- Searching on windows the “cmd” name an open as administrator
- Navigate to your path where you need to rename the file by type cd and the path
- Click Enter
- Execute the following command
rename "TESTA.txt" "TESTB.txt"
Note! The quotation marks in the command are only required if the name includes spaces.
The above command will rename the file name “TESTA.txt” to “TESTB.txt”. You need to be located at the CMD on the folder where the file is.
If the “TESTA.txt” file is not located in your current directory, you must specify the path to the file as a prefix to the file name. For example, if the file was in the “C:” directory, you would type a command similar to the following example.
rename “c:\TESTA.txt” “TESTB.txt”
Since the “rename” command can address extensions, you can also use it to change the extensions of the file.
Rename a file keeping the original
Use the following command at the Windows command line or within a batch file.
xcopy "TESTA.txt" "TESTB.txt"
The command will create a copy of the original files with the new extension.
Rename a single file with the move command
Like using the rename command, you can also use the move command to rename a file as shown.
move "TESTA.txt" "TESTB.txt"
What you need to know:
- If the files are being used by a program, then the rename command fails with the below error.
The process cannot access the file because it is being used by another process.
- You also need to have sufficient privileges to rename the file.
Access is denied.
- Keep in mind that the rename changes just the file name, it does not convert a file from one type to another. For example, renaming a pdf file ‘test1.pdf’ to ‘test1.docx does not make the file readable in Microsoft Word.
How to create a simple batch script to rename the file
If you are going to automate the above activities or to execute over the network you will need to create a batch file. For example, you need to rename the same file on all computers of the network:
- Open a notepad file
- Copy the below command
@echo off rename “c:\TESTA.txt” “TESTB.txt”
- Save as rename.bat
- Execute the file and the file “TESTA.txt” will be changed to “TESTB.txt”
How t0 rename the file using PowerShell:
PowerShell offers even more flexibility for renaming files in a command-line environment. Using PowerShell, you can pipe the output of one command—known as a “commandlet” in PowerShell terms—to another command, just like you can on Linux and other UNIX-like systems. The two important commands you’ll need are Dir, which lists the files in the current directory, and Rename-Item, which renames an item (a file, in this case). Pipe the output of Dir to Rename-Item and you’re in business.
- Open Start.
- Search for PowerShell and click the top result to open the app.
- Type the following command to rename a single file and press Enter:
rename-item "TESTA.txt" "TESTB.txt"
Advanced features of “Rename” Commands
Below you will find some undocumented features of “Rename” commands to chop off everything from a file name after the last occurrence of a specified character:
REN TESTA.txt *S
Will rename TESTA.txt to TES.
REN TESTA.txt *T
Will not change the name at all (remember: the last occurrence…?).
REN TESTA.txt *ST
Will rename TESTA.txt to TEST.
REN TESTA.txt *sa
Will rename TESTA.txt to TESA, so it seems to mean chop off everything after the last S and then append an A.
This is all about some methods and tricks to play with the “rename” or “ren” command. The first goal was to create batch script rename file using Command-Line.