2017-05-05 251 views
0

我已经看过这个问题在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!" 
) 

可有人请帮我调整代码?

回答

0

也许这样的事情会做:

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以包含完整路径,则需要调整令牌化以迎合额外的分隔符

+0

感谢您的帮助,第二个代码运行良好,唯一的一点是'example.com'中的'é' - BannièresMobile 300X250 VF(300x250)'显示出像这样的'Banni├¿res' 其他比一切都好,非常感谢你的帮助 – Penguin90x