Renaming Files
In everyday computer use, there are many opportunities to change the file name of a specific file.
here may also be cases where you want to change the file names of multiple files according to a specific rule.
On this page, we will introduce how to change the file name of one or more files using wildcards or sequential numbers with concrete examples, using the command prompt.
You can also change file names using batch files in the same way.
Commands to use
To change the file name, use the rename
command. You can also get the same result by entering ren
instead.
By specifying the current file name as the first argument and the desired file name as the second argument, you can change the file name.
Executing from the command prompt
This is an example of renaming a file named “before.txt” saved in My Documents to “after.txt”.
Executing from a batch file
@echo off
setlocal
set FOLDER=%userprofile%\Documents\
rename %FOLDER%before.txt after.txt
endlocal
exit
Renaming Multiple Files at Once
With the rename
command, you can specify wildcards in the first and second arguments to rename multiple files at once.
Here are some examples of how to use this feature.
Renaming with Wildcards
The following command renames the extension of “before.txt” in the “disposable” folder to .html
.
Renaming with for
loop
When you want to add a specific string to the beginning of multiple file names, the rename
command alone cannot solve the problem. In this case, you can use a for
loop to achieve the desired result.
The following command is an example of adding “renamed_” to the beginning of all file names saved in the current directory.
Note that this command does not apply to folders.
In the above code, both "%f"
and "renamed_%f"
, which are specified as arguments for the rename
command, can be executed without double quotes.
However, if the file name contains a space, the rename will not be executed without double quotes and will result in a syntax error.
Considering the possibility that the target file may contain a space, it is recommended to use double quotes.
For example, consider the following folder structure:
C:\test
│
└─disposable
sample01.txt
sample02.txt
sample03.txt
sample04.txt
When you execute the above command with C:\test\disposable
as the current directory, the output will be as follows.
When you check the folder structure, you can see that the file names have been changed as follows:
C:\test
│
└─disposable
renamed_sample01.txt
renamed_sample02.txt
renamed_sample03.txt
renamed_sample04.txt