在内容中使用字符串重命名文本文件

Renaming text files using a string in their content

我在一个 3 年前的问题中查看了这个问题,但无法调整批处理脚本来处理我的案例:Rename text files based on multiple strings in their contents

这是我的文本文件包含的内容:

<!-- ===============================================
TEXT TEXT"
===============================================
An : ONE
Ca : ONE - AVRIL 2016 
Site : example.com -->

<!--===============================================
Insertion : example.com - Bannières Mobile 300X250 VF (300x250)

我想用 Insertion : 行包含的内容重命名每个文件。在我的示例中它将是:example.com - Bannières Mobile 300X250 VF (300x250)

我尝试使用这个脚本,因为它使用了日期和时间,这是最初的问题,但我不知道如何让它工作

setlocal enabledelayedexpansion
REM for all files do:
for %%f in (*.txt) do (
  REM get desired lines and set variables:
  for /f "tokens=2,4 delims=:" %%i in (' findstr "^Insertion" %%f') do set %%i=%%j
  REM construct filename:
  set "name=!Insertion!.txt"
  echo Filename is: "!name!"
)

有人可以帮我修改代码吗?

也许这样的事情会做:

For /F "Tokens=1,2*Delims=:" %%A In ('FindStr/LBIC:"Insertion : " *.txt'
) Do For /F "Tokens=*" %%D In ("%%~C"
) Do If Not Exist "%%~D%%~xA" Ren "%%A" "%%~D%%~xA"

如果遇到循环问题,(循环读取刚刚重命名的文件),然后添加一个额外的小步骤:

For /F "Tokens=1,2*Delims=:" %%A In ('FindStr/LBIC:"Insertion : " *.txt'
) Do For /F "Tokens=*" %%D In ("%%~C"
) Do If Not Exist "%%~D%%~xA_" Ren "%%A" "%%~D%%~xA_"
Ren *.txt_ *.txt

注意:如果您将 *.txt 更改为包含完整路径,则需要调整标记化以适应额外的分隔符