【问题标题】:Renaming text files using a string in their content使用内容中的字符串重命名文本文件
【发布时间】:2017-05-06 03:11:11
【问题描述】:

我在一个 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!"
)

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

【问题讨论】:

    标签: batch-file file-rename


    【解决方案1】:

    也许这样的事情会做:

    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 更改为包含完整路径,则需要调整标记以适应额外的分隔符

    【讨论】:

    • 感谢您的帮助,第二个代码运行良好,唯一的问题是example.com - Bannières Mobile 300X250 VF (300x250) 中的é 之类的字符显示为这样Banni├¿res 除此之外一切都很好,非常感谢非常感谢您的帮助
    猜你喜欢
    • 2012-05-28
    • 2018-07-16
    • 1970-01-01
    • 2017-10-11
    • 2014-02-13
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    相关资源
    最近更新 更多