【问题标题】:Search file names from one list and replace them with another list in BATCH从一个列表中搜索文件名并将它们替换为 BATCH 中的另一个列表
【发布时间】:2019-02-15 00:29:16
【问题描述】:

我有两个文件:

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 ?

【问题讨论】:

  • 你必须在两个不同的文件中有文件名吗?为什么不将它们放在 1 个文件中?似乎您想批量执行ren file1 file2,如果您已经知道文件名是什么,只需创建一个以空格分隔的文件。即file1.txt file2.txt 并做for /f "tokens=1,2" %%i in (names.txt) do ren "%%i" "%%j"
  • 我尝试使用for /f "tokens=1,2" %%i in (test.txt) do ren "%%i" "%%j" 并没有发生任何事情,我错过了一行吗?
  • 你在test.txt中有两个文件名?您要重命名的文件是否与脚本位于同一目录中?
  • 是的,我愿意,test.txt 就是这样:dummy replacement,我有一个名为 tmp_dummy.jpg 的文件。它说找不到文件。
  • 不,那样不行。如果您想更换零件,那么您问的问题不正确。让我补充一下。

标签: batch-file rename batch-rename


【解决方案1】:

根据我的评论。创建一个名为 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

【讨论】:

    【解决方案2】:

    对于您的文件对概念,如果我理解正确,您基本上需要同时逐行读取两个文件,这可以像this answer 中说明的那样完成。所以这是一个可能的代码:

    @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 命令!

    如果两个名称文件中出现以下任何字符,这将不起作用:=、!、^; ~ 不得作为第一个文件(旧名称)的任何行中的第一个字符出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 2020-03-20
      • 2018-06-06
      • 2011-08-28
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      相关资源
      最近更新 更多