
我正在運行批次腳本來處理一些文件。當它確定當前文件不需要處理時,我想跳過該文件的其餘命令並直接移至下一個。如何?Goto :EOF
並且exit /b
都退出整個腳本。
這是腳本。它需要一個目錄並重新命名所有檔案以包含父目錄名稱中的所有單字(檔案名稱中已包含的單字除外)。
@echo off
setlocal enabledelayedexpansion
rem set input directory (add check for validity)
set "inputDir=%~1"
set "confirmation=false"
:proceed
for /r "%inputDir%" %%F in (*.*) do (
rem use path to file as keywords
set "words=%%~pF"
rem replace known useless marks with spaces
set "words=!words:\= !"
set "words=!words:_= !"
set "words=!words:,= !"
set "words=!words: = !"
rem remove known useless words
set "words=!words: The = !"
set "words=!words: A = !"
set "words=!words: An = !"
set "words=!words: Video = !"
set "words=!words: New Folder = !"
rem remove word that already exists in the filename
for %%W in (!words!) do echo %%~nF | find /i "%%W" >nul && set "words=!words: %%W = !"
rem replace leading and trailing spaces with brackets
set "words=[!words:~1,-1!]"
rem if all words already included end task for current file
if "!words!"=="[]" exit /b
rem build the new filename
set "newName=%%~nF !words!%%~xF"
rem fix "] [" caused by repeated renaming, causes fusion with non-related bracketed sets, which is fine
set "newName=!newName:] [= !"
rem task for displaying the name change for confirmation
if /i not "%confirmation%"=="yes" echo old "%%F" && echo new "!newName!"
rem task for doing the actual rename if confirmed
if /i "%confirmation%"=="yes" echo ren "%%~nxF" "!newName!" & echo ren "%%F" "!newName!"
)
rem fails to rename files with ! mark in the filename. no other trouble, just won't rename those. error: the syntax of the command is incorrect
rem if coming from second (completed) run then exit
if /i "%confirmation%"=="yes" exit /b
rem ask for confirmation to run again for real
set /p confirmation="confirm you want to perform all these rename tasks (type yes or no)?: "
if /i not "%confirmation%"=="yes" echo confirmation denied
if /i "%confirmation%"=="yes" goto proceed
endlocal
我也在嘗試一種有條件地運行最後命令的替代方法,如下所示
@echo off
setlocal enabledelayedexpansion
rem set input directory (add check for validity)
set "inputDir=%~1"
set "confirmation=false"
:proceed
for /r "%inputDir%" %%F in (*.*) do (
rem use path to file as keywords
set "words=%%~pF"
rem replace known useless marks with spaces
set "words=!words:\= !"
set "words=!words:_= !"
set "words=!words:,= !"
set "words=!words: = !"
rem remove known useless words
set "words=!words: The = !"
set "words=!words: A = !"
set "words=!words: An = !"
set "words=!words: Video = !"
set "words=!words: New Folder = !"
rem remove word that already exists in the filename
for %%W in (!words!) do echo %%~nF | find /i "%%W" >nul && set "words=!words: %%W = !"
rem replace leading and trailing spaces with brackets
set "words=[!words:~1,-1!]"
rem if all words not already included in the filename do
if not "!words!"=="[]" (
rem build the new filename
set "newName=%%~nF !words!%%~xF"
rem fix "] [" caused by repeated renaming, causes fusion with non-related bracketed sets, which is fine
set "newName=!newName:] [= !"
rem task for displaying the name change for confirmation
if /i not "%confirmation%"=="yes" echo old "%%F" && echo new "!newName!"
rem task for doing the actual rename if confirmed
if /i "%confirmation%"=="yes" echo ren "%%~nxF" "!newName!" & echo ren "%%F" "!newName!"
)
)
rem fails to rename files with ! mark in the filename. no other trouble, just won't rename those. error: the syntax of the command is incorrect
rem if coming from second (completed) run then exit
if /i "%confirmation%"=="yes" exit /b
rem ask for confirmation to run again for real
set /p confirmation="confirm you want to perform all these rename tasks (type yes or no)?: "
if /i not "%confirmation%"=="yes" echo confirmation denied
if /i "%confirmation%"=="yes" goto proceed
endlocal
但這是一場災難,導致檔案名稱被從檔案名稱中看似隨機的部分中刪除並words
添加了。
答案1
只需放置一個標籤:nextiteration
(或您喜歡的任何其他名稱)作為 for 循環體內的最後一個語句,並goto
在您決定可以跳過 for 循環體的其餘部分時放置到該標籤。
因為 FOR 主體上的右括號前面必須有一個常規語句(標籤不行),所以您必須在標籤和右括號之間放置一個虛擬語句(如 REM)。
FOR xxxxxxxxx DO (
some statements (executed on each iteration)
IF {some condition is met} GOTO nextiteration
statements that will be skipped if conditon is met
:nextiteration
REM to prevent syntax error
)
答案2
Windows 批次處理沒有太多用於跳過循環迭代的選項。您可以使用的方法有:
在您想要跳過的循環部分放置一個 IF 區塊:
@Echo Off if A equ A ( IF B==C ( Echo shouldn't go here ) )
用於
call
呼叫包含整個內部循環的例程,其中exit/B
將起作用:@Echo Off Setlocal For %%A in (alpha beta gamma) DO ( Echo Outer loop %%A Call :inner ) Goto :eof :inner For %%B in (U V W X Y Z) DO ( if %%B==X ( exit /b 2 ) Echo Inner loop Outer=%%A Inner=%%B )
使用命令
goto :Label
並將其放在Label:
循環的末尾。由於已知的錯誤,這可能不適用於您的情況:在括號內使用 GOTO(包括 FOR 和 IF 命令)會破壞它。
顯示錯誤的範例:@Echo Off if A equ A ( IF B==C GOTO specialcase Echo shouldn't go here goto:eof :specialcase )
參考 :
答案3
嘗試使用For /F...
和... | findstr /c:"string1" /c:"string2"...
使用允許您過濾字串的循環,從而更好地控制生成的 interrese 檔案名稱的操作,這使得您可以更輕鬆地進行交互以重命名它們,而無需嘗試跳過循環,因為它變得更加精確:
@echo off & setlocal
2>nul cd /d "%~1" || exit
setlocal enabledelayedexpansion
for %%G in =;(
"_",","," "," The "," A "," An "," Video "," New Folder "
);= do if not defined _str2find =;( set "_str2find=/c:"%%~G""
);= else call set "_str2find=!_str2find! /c:"%%~G""
setlocal disabledelayedexpansion
for /f usebackq^delims^= %%G in (`
2^>nul where /r . *.* ^| findstr %_str2find% `);= do =;(
echo/%%~G
:: So you don't need to use skip, the rest of your
:: interactions continue here and within your loop
);=
endlocal