从一个列表中搜索文件名并将其替换为 BATCH 中的另一个列表
Search file names from one list and replace them with another list in BATCH
我有两个文件:
old_names.txt
oldname1
oldname2
new_names.txt
newname1
newname2
我想在我的文件夹中搜索包含 "old_names" 的文件名 并将 "old_name" 字符串替换为相应的 "new_name".
for /f %%i in (old_names.txt) do (
// get corresponding %%j new_name
for /f %%a in ('dir /b /a-d *%%i*') do ren %%a %%j
)
如何检索相应的 new_name?
根据我的评论。创建一个名为 names.txt
的文件并添加要替换的字符串以及要替换的内容:
dummy replacement
dummy2 replacement2
然后脚本需要在同一目录中,或者您必须指定文件的路径:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2" %%i in (names.txt) do (
for /f %%a in ('dir /b /a-d ^| findstr "%%i"') do (
set "oldname=%%a"
set "newname=!oldname:%%i=%%j!"
echo ren "!oldname!" "!newname!"
)
)
或通过指定路径:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2" %%i in (names.txt) do (
for /f %%a in ('dir /b /a-d "D:\PATH" ^| findstr "%%i"') do (
set "oldname=%%a"
set "newname=!oldname:%%i=%%j!"
echo ren "!oldname!" "!newname!"
)
)
一旦您满意它会将要替换的文件打印到屏幕上,只需从最后一行代码中删除 echo
即可实际执行 ren
对于你的文件对概念,你基本上需要同时逐行读取两个文件,如果我理解正确的话,这可以像 中说明的那样完成。所以这是一个可能的代码:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=D:\path\to\root_dir" & rem // (directory containing files to rename)
set "_FILE1=D:\path\to\old_names.txt"
set "_FILE2=D:\path\to\new_names.txt"
rem // Count number of lines of 1st file (2nd file is not checked):
for /F %%C in ('^< "%_FILE1%" find /C /V ""') do set "NUM1=%%C"
setlocal EnableDelayedExpansion
rem // Change into root directory:
cd /D "%_ROOT%" || exit /B 1
rem // Here input redirection is used to read both files simultaneously:
9< "!_FILE1!" 8< "!_FILE2!" (
rem // Loop through the number of lines of the 1st file:
for /L %%I in (1,1,%NUM1%) do (
rem // Read a line from the 1st file:
<&9 (set "LINE1=" & set /P "LINE1=")
rem // Read a line from the 2nd file:
<&8 (set "LINE2=" & set /P "LINE2=")
rem /* Assign line strings to `for` variables to later avoid nested
rem delayedly expanded environment variables: */
for /F "tokens=1,2 delims=| eol=|" %%I in ("!LINE1!|!LINE2!") do (
rem // Get list of files matching the partial name:
for /F "delims= eol=|" %%F in ('dir /B /A:-D "*!LINE1!*"') do (
endlocal
rem // Store current file name:
set "NAME=%%F"
setlocal EnableDelayedExpansion
rem // Do the actual sub-string replacement and rename file:
ECHO ren "!NAME!" "!NAME:%%I=%%J!"
)
)
)
)
endlocal
endlocal
exit /B
检查正确输出后,删除大写 ECHO
命令!
如果以下任何字符出现在两个名称文件中,这将不起作用:=
、!
、^
; ~
不得作为第一个文件(旧名称)的任何行中的第一个字符出现。
我有两个文件:
old_names.txt
oldname1
oldname2
new_names.txt
newname1
newname2
我想在我的文件夹中搜索包含 "old_names" 的文件名 并将 "old_name" 字符串替换为相应的 "new_name".
for /f %%i in (old_names.txt) do (
// get corresponding %%j new_name
for /f %%a in ('dir /b /a-d *%%i*') do ren %%a %%j
)
如何检索相应的 new_name?
根据我的评论。创建一个名为 names.txt
的文件并添加要替换的字符串以及要替换的内容:
dummy replacement
dummy2 replacement2
然后脚本需要在同一目录中,或者您必须指定文件的路径:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2" %%i in (names.txt) do (
for /f %%a in ('dir /b /a-d ^| findstr "%%i"') do (
set "oldname=%%a"
set "newname=!oldname:%%i=%%j!"
echo ren "!oldname!" "!newname!"
)
)
或通过指定路径:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2" %%i in (names.txt) do (
for /f %%a in ('dir /b /a-d "D:\PATH" ^| findstr "%%i"') do (
set "oldname=%%a"
set "newname=!oldname:%%i=%%j!"
echo ren "!oldname!" "!newname!"
)
)
一旦您满意它会将要替换的文件打印到屏幕上,只需从最后一行代码中删除 echo
即可实际执行 ren
对于你的文件对概念,你基本上需要同时逐行读取两个文件,如果我理解正确的话,这可以像
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=D:\path\to\root_dir" & rem // (directory containing files to rename)
set "_FILE1=D:\path\to\old_names.txt"
set "_FILE2=D:\path\to\new_names.txt"
rem // Count number of lines of 1st file (2nd file is not checked):
for /F %%C in ('^< "%_FILE1%" find /C /V ""') do set "NUM1=%%C"
setlocal EnableDelayedExpansion
rem // Change into root directory:
cd /D "%_ROOT%" || exit /B 1
rem // Here input redirection is used to read both files simultaneously:
9< "!_FILE1!" 8< "!_FILE2!" (
rem // Loop through the number of lines of the 1st file:
for /L %%I in (1,1,%NUM1%) do (
rem // Read a line from the 1st file:
<&9 (set "LINE1=" & set /P "LINE1=")
rem // Read a line from the 2nd file:
<&8 (set "LINE2=" & set /P "LINE2=")
rem /* Assign line strings to `for` variables to later avoid nested
rem delayedly expanded environment variables: */
for /F "tokens=1,2 delims=| eol=|" %%I in ("!LINE1!|!LINE2!") do (
rem // Get list of files matching the partial name:
for /F "delims= eol=|" %%F in ('dir /B /A:-D "*!LINE1!*"') do (
endlocal
rem // Store current file name:
set "NAME=%%F"
setlocal EnableDelayedExpansion
rem // Do the actual sub-string replacement and rename file:
ECHO ren "!NAME!" "!NAME:%%I=%%J!"
)
)
)
)
endlocal
endlocal
exit /B
检查正确输出后,删除大写 ECHO
命令!
如果以下任何字符出现在两个名称文件中,这将不起作用:=
、!
、^
; ~
不得作为第一个文件(旧名称)的任何行中的第一个字符出现。