2017-05-05 250 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