Write a run .bat file
Create a new text document and enter the following content:
@echo off chcp 65001 setlocal enabledelayedexpansion rem Set the string to find and replace set "search=aaa" set "replace=bbb" rem sets file name matching mode, for example *.txt means modifying .txt files, *.* means modifying all files set "filePattern=*.txt" rem sets the path to the target folder set "folderPath=C:\YourFolderPath" rem switch to the destination folder cd /d "%folderPath%" rem Iterate over the file and rename it for %%f in (%filePattern%) do ( set "filename=%%~nf" set "newFilename=!filename:%search%=%replace%!" ren "%%f" "!newFilename!%%~xf" ) echo File name modification completed。 pause
Follow these steps to execute this batch script:
- Open Notepad or other text editor and copy and paste the above code into the newly created text file.
- In the code,
search
Set the variable to the string to be searched, andreplace
The variable is set to the string to be replaced. - If you need to modify a specific type of file, please turn
filePattern
The variable is set to the corresponding file extension. If you want to modify all files, you can set them to*.*
。 - Will
folderPath
The variable is set to the path to the destination folder where the file is to be renamed. - Save text files as batch files, e.g.
rename_files.bat
。 - Double-click the batch file to execute the script, which will iterate over the files in the target folder and rename them according to the rules you specified.
Please be careful and back up the files before execution in case you need to restore them.
folderPath is set to the folder where the bat file resides
@echo off chcp 65001 setlocal enabledelayedexpansion rem Get the folder path where the batch file resides for %%I in ("%~dp0.") do set "folderPath=%%~fI" rem sets the string to find and replace set "search=aaa" set "replace=bbb" rem sets file name matching mode, for example *.txt means modifying .txt files, *.* means modifying all files set "filePattern=*.txt" rem switch to the destination folder cd /d "%folderPath%" rem Iterate over the file and rename it for %%f in (%filePattern%) do ( set "filename=%%~nf" set "newFilename=!filename:%search%=%replace%!" ren "%%f" "!newFilename!%%~xf" ) echo File name modification completed。 pause
In the above code,"%~dp0"
Used to obtain the folder path where the batch file is located and then assign it tofolderPath
variables for use in subsequent operations. This way, the script will work in the folder where the batch file is located, where you can perform file name modification operations.
Code explanation
for %%I in ("%~dp0.") do set "folderPath=%%~fI"
The purpose of this batch script is to get the path to the folder containing the batch file and store the path under the namefolderPath
in the variable. Let me explain a few key parts:
-
%~dp0
: This is a special variable built into the batch file, which represents the full path to the current batch file, where%~d
Indicates the drive,%~p
Indicates the path. therefore,%~dp0
Indicates the folder path of the current batch file. -
for %%I in ("%~dp0.")
: This is onefor
loop, iterates over an item list where%%I
It is a loop variable. Here we usefor
Change to handle%~dp0
The value of because%~dp0
It may contain spaces or special characters and needs to be enclosed in quotes. -
set "folderPath=%%~fI"
:existfor
Inside the loop,%%~fI
Represents the loop variable%%I
Expand, where%~f
Indicates the complete path to obtain. therefore,set "folderPath=%%~fI"
WillfolderPath
The variable is set to the full path to the folder containing the batch file.
In short, the purpose of this code is to get the complete path to the folder where the batch file is located and store it infolderPath
variable so that the path is used in subsequent parts of the script. This is useful for situations where the script is required to reference the folder in a batch script, as it allows the script to access its required resources or files without considering the specific location.
for %%f in (%filePattern%) do
for %%f in (%filePattern%) do
is a loop structure in batch processing. Its purpose is to traverse files that meet the specified file pattern and execute a set of commands on each file. Let me explain a few key parts:
for %%f
: This is onefor
The beginning of the loop, where%%f
is the name of the loop variable. In batch processing, it is usually used%%
to represent loop variables, not individual%
。in (%filePattern%)
: This section defines the list of files to be traversed.%filePattern%
is a variable that should contain a file pattern to match the files to be processed. For example, if%filePattern%
The value of*.txt
, then the loop will traverse all extensions as.txt
file.do
:existdo
What follows is a set of commands to be executed in each iteration. These commands can be any valid command in a batch script, such as renaming a file, copying a file, performing an operation, etc.
Together,for %%f in (%filePattern%) do
The function is to traverse and satisfy the file mode%filePattern%
file and execute on each filedo
The following set of commands. This enables the batch script to perform the same operations on a set of files, such as batch renaming, copying or deleting files, etc.
set "filename=%%~nf"
set "newFilename=!filename:%search%=%replace%!"
ren "%%f" "!newFilename!%%~xf"
The code in this batch file is used to rename parts of the text in the file name. Let me explain the meaning of each line of code:
-
set "filename=%%~nf"
: This line of code stores the name of the current file (excluding the extension) in a variablefilename
middle.%%~nf
is a special syntax in a batch file to get the file name part, where%%~n
Indicates obtaining the file name,f
A placeholder for the current file. so%%~nf
will be replaced with the name of the current file. -
set "newFilename=!filename:%search%=%replace%!"
: This line of code will be infilename
Find and replace text in variables. Specifically, it will look forfilename
All in%search%
text and replace it with%replace%
text.!filename:%search%=%replace%!
It is the syntax used in batch files for text replacement. -
ren "%%f" "!newFilename!%%~xf"
: This line of code usesren
Command (abbreviated asrename
) to rename the file.%%f
The placeholder for the current file.!newFilename!
It is the new file name calculated in the previous line of code.%%~xf
Indicates the extension of the current file. Therefore, this line of code takes the name of the current file from%%f
Change to!newFilename!
, and retain the extension of the original file.
In short, this batch file is used to find specific text in the file name (%search%
) and replace it with other text (%replace%
) and rename the file to the new file name while retaining the original file's extension.
bat's output terminal garbled code
If you see garbled code in the terminal while running the batch file, this may be caused by a character set or encoding issue.
Character set mismatch: Make sure both the terminal and batch files use the same character set, usually UTF-8. You can set the character set by adding the following line at the beginning of the batch file:
@echo off chcp 65001 (Set the character set to UTF-8)
This is the article about using examples of using bat batch modifying file names in Windows system. For more related contents of bat batch modifying file names, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!