2016-11-09 216 views
0

我做了一個批處理腳本來重命名大量的文件。它取出它們的名字並在文本文檔中搜索它,複製該行並從中獲取我需要的數據,然後重命名該文件。Windows批量重命名文本文件

它似乎大部分工作正常,但我不能檢查它是如何做的,因爲它不斷地在控制檯中產生錯誤/警告。

@echo off 
set ogg=.ogg 
Setlocal EnableDelayedExpansion 
for %%a in (*.ogg) do (
    set fileNameFull=%%a 
    set fileName=!fileNameFull:~0,-4! 
    for /F "delims=" %%a in ('findstr /I !fileName! strings.txt') do (
     endlocal 
     set "stringLine=%%a%ogg%" 
    ) 
    Setlocal EnableDelayedExpansion 
    set fullString=!stringLine:~26! 
    ren %%a "!fullString!" 
) 

pause 

代碼的工作,我只是想能夠跟蹤進展情況,文件10,000s都在同一時間被重命名和我不遠處沿着過程是如何指示。

的錯誤是:

"FINDSTR: Cannot open [...]" 
"The syntax of the command is incorrect." 

回答

0
@echo off 
Setlocal EnableDelayedExpansion 
for %%a in (*.ogg) do (
    for /F "delims=" %%q in ('findstr /I /L /c:"%%~na" strings.txt') do (
    set "stringLine=%%q" 
    ) 
    ECHO ren "%%a" "!stringLine:~26!.ogg" 
) 

pause 

此代碼應該是等價的,但固定的,以你的代碼已經發布。

修正:

Removed the endlocal/setlocal complication - not required 
changed the inner `for` metavariable - must not be duplicate `%%a` 
Changed the `findstr` switches - add `/L` for literal and `/c:` to force single token in case of a separator-in-name; use `%%~na` to specify "the name part of `%%a`" to avoid the substringing gymnastics. 
removed said gymnastics 
Removed 2-stage string manipulation of destination filename 
Removed superfluous setting of `ogg` 

得到的代碼應該重複你所擁有的最初,但它只會報告rename指令。你應該對一個小的代表性樣本進行測試來驗證。

計數/進度:

set /a count=0 
for %%a in (*.ogg) do (
    for /F "delims=" %%q in ('findstr /I /L /c:"%%~na" strings.txt') do (
    set "stringLine=%%q" 
    ) 
    ECHO ren "%%a" "!stringLine:~26!.ogg" 
    set /a count +=1 
    set /a stringline= count %% 1000 
    if %stringline% equ 0 echo !count! Processed 
) 

pause 

應該表現出你的進步每1000

你可以使用

if %stringline% equ 0 echo !count! Processed&pause 

進展之前等待用戶動作...

順便說一句 - 我假設你的文件中的第27+列是新名稱,s因此,您還沒有向我們展示一個示例。此外,您應該知道,一個簡單的findstr會將目標字符串定位爲文件中任何位置的子字符串 - 可以是新名稱或舊名稱。如果您調用findstr上的/B開關,則字符串將僅在行的最開始處匹配。

+0

感謝您的回覆,但輕微的問題。我必須禁用DelayedExpansion,因爲「!」在從文本文件複製的字符串內被刪除。我認爲這是一個問題,但我不知道替代方案是什麼。 – Adamj200

+0

這就是爲什麼你應該提供樣本代表性數據。它可以 - 也將在這種情況下 - 改變整個方法。 – Magoo