我正在运行批处理脚本来处理一些文件。当它确定当前文件不需要处理时,我想跳过该文件的其余命令并直接移至下一个。如何?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
添加了。
只需放置一个标签
:nextiteration
(或您喜欢的任何其他名称)作为 for 循环主体内的最后一个语句,并goto
在您决定跳过 for 主体的其余部分时放置到该标签。Windows 批处理没有太多用于跳过循环迭代的选项。您可以使用的方法有:
在您想要跳过的循环部分放置一个 IF 块:
用于
call
调用包含整个内部循环的例程,其中exit/B
将起作用:使用该命令
goto :Label
并将其放在Label:
循环的末尾。由于已知的错误,这可能不适用于您的情况:在括号内使用 GOTO(包括 FOR 和 IF 命令)会破坏它。显示错误的示例:
参考 :