Windows system uses bat to batch modify file names

Windows systems use bat files to batch modify file names.

Write and run .bat file

Create a new text document and enter the following content:

@echo off
chcp 65001
setlocal enabledelayedexpansion

rem 设置要查找和替换的字符串
set "search=aaa"
set "replace=bbb"

rem 设置文件名匹配模式,例如*.txt表示修改.txt文件,*.*表示修改所有文件
set "filePattern=*.txt"

rem 设置目标文件夹的路径
set "folderPath=C:\YourFolderPath"

rem 切换到目标文件夹
cd /d "%folderPath%"

rem 遍历文件并重命名
for %%f in (%filePattern%) do (
    set "filename=%%~nf"
    set "newFilename=!filename:%search%=%replace%!"
    ren "%%f" "!newFilename!%%~xf"
)

echo 文件名修改完成。
pause

Please follow the steps below to execute this batch script:

  1. Open Notepad or another text editor and copy and paste the above code into the newly created text file.
  2. In your code,  search set the variable to the string you want to find and  replace the variable to the string you want to replace.
  3. If you need to modify a specific type of file, set  filePattern the variable to the corresponding file extension. If you want to modify all files, you can set it to  *.*.
  4. Set  folderPath the variable to the path of the destination folder containing the files you want to rename.
  5. Save the text file as a batch file, eg  rename_files.bat.
  6. Double-click the batch file to execute the script, which will loop through the files in the target folder and rename them according to the rules you specify.

Please proceed with caution and back up your files before proceeding in case you need to restore.

folderPath is set to the folder where the bat file is located

@echo off
chcp 65001
setlocal enabledelayedexpansion

rem 获取批处理文件所在的文件夹路径
for %%I in ("%~dp0.") do set "folderPath=%%~fI"

rem 设置要查找和替换的字符串
set "search=aaa"
set "replace=bbb"

rem 设置文件名匹配模式,例如*.txt表示修改.txt文件,*.*表示修改所有文件
set "filePattern=*.txt"

rem 切换到目标文件夹
cd /d "%folderPath%"

rem 遍历文件并重命名
for %%f in (%filePattern%) do (
    set "filename=%%~nf"
    set "newFilename=!filename:%search%=%replace%!"
    ren "%%f" "!newFilename!%%~xf"
)

echo 文件名修改完成。
pause

In the above code, "%~dp0" it is used to obtain the folder path where the batch file is located, and then assign it to  folderPath a variable for use in subsequent operations. This way the script will work in the same folder as the batch file, where you can perform the 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 that path in a  folderPath variable named. Let me explain a few key parts of it:

  1. %~dp0: This is a special variable built into the batch file. It represents the full path of the current batch file, where  %~d represents the drive and %~p represents the path. Therefore, %~dp0 represents the folder path of the current batch file.

  2. for %%I in ("%~dp0."): This is a  for loop that iterates over a list of items, where  %%I is the loop variable. Here, we use  for a loop to process  %~dp0 values ​​that  %~dp0 need to be enclosed in quotes as they may contain spaces or special characters.

  3. set "folderPath=%%~fI"for Inside the loop, %%~fI it represents the expansion of the loop variable  %%I , which  %~f means obtaining the complete path. Therefore, set "folderPath=%%~fI" set  folderPath the variable to the full path of the folder containing the batch file.

In summary, what this code does is get the full path to the folder where the batch file is located and store it in  folderPath a variable so that it can be used in subsequent parts of the script. This is useful for situations where you need to reference the folder where the script is located in a batch script, as it allows the script to access the resources or files it needs regardless of 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 the files that meet the specified file pattern and execute a set of commands on each file. Let me explain a few key parts of it:

  • for %%f: This is  for the beginning of a loop, where  %%f is the name of the loop variable. In batch processing, it is often used  %% to represent loop variables instead of individual ones  %.

  • in (%filePattern%): This part defines the list of files to be traversed. %filePattern% is a variable that should contain a file pattern that matches the file to be processed. For example, if  %filePattern% the value is  *.txt, then the loop will loop through all files with extension  .txt .

  • dodo What follows is a set of commands to be executed on each iteration. These commands can be any valid command in a batch script, such as renaming files, copying files, performing operations, etc.

Taken together, for %%f in (%filePattern%) do the function is to traverse  %filePattern% files that satisfy the file pattern and execute  do the following set of commands on each file. This enables batch scripts to perform the same operation on a group 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 part of the text in the file name. Let me explain what each line of code means:

  1. set "filename=%%~nf": This line of code stores the name of the current file (excluding the extension) in a variable filename. %%~nf It is a special syntax used in a batch file to obtain the file name part, which %%~nrepresents obtaining the file name and frepresents the placeholder of the current file. So %%~nf will be replaced with the name of the current file.

  2. set "newFilename=!filename:%search%=%replace%!": This line of code will filenamefind and replace text in a variable. Specifically, it finds filenameall text in %search%and replaces it with %replace%text. !filename:%search%=%replace%! Is the syntax used for text replacement in batch files.

  3. ren "%%f" "!newFilename!%%~xf": This line of code uses renthe command (abbreviated to rename) to rename the file. %%f A placeholder representing the current file. !newFilename! is the new file name calculated in the previous line of code and %%~xf represents the extension of the current file. So this line of code %%fchanges the name of the current file from to !newFilename!, and retains the extension of the original file.

In summary, this batch file is used to find specific text in a file name ( %search%) and replace it with other text ( %replace%), then rename the file to the new file name while retaining the extension of the original file.

The output terminal of bat is garbled

If you see garbled characters in the terminal when running a batch file, this may be due to a character set or encoding issue.

Character set mismatch : Make sure both the terminal and the batch file are using the same character set, usually UTF-8. The character set can be set by adding the following line at the beginning of the batch file:

@echo off
REM 将字符集设置为 UTF-8
chcp 65001

Summarize

 Windows systems use bat files to batch modify file names.

Guess you like

Origin blog.csdn.net/weixin_56337147/article/details/133639059