我有一个包含多行的文件:
"pathToFile=%~dp0PBreport\cpy\Install\"
更多,我有一个具有不同搜索值的字符串列表:
"searchValue=abc,there,rtz"
我的目标是在文件的每一行中搜索是否存在搜索值,然后执行一些代码。但是,如果也许找到了我的搜索值列表中的第一个元素,则不必检查其他值。我使用嵌套的foor循环来完成整个过程,但是当我实现GOTO
之类的东西时,外部循环不会继续。在循环中使用GOTO是否有一些错误?还是有更好的方法退出并跳过所有其他检查(如果已找到)?没有GOTO
陈述,完整的代码可以正常工作。
FOR /f "tokens=1 delims=" %%G in (%pathToFile%) do (
set "dataRow=%%~G"
FOR %%I in (%searchValue%) do (
IF not "!dataRow:%%I=!"=="!dataRow!" (
### some Code here ###
GOTO endIfCases
)
)
###some similiar blocks like the one above which can also be ignored if one match was found beforehand###
:endIfCases
### some Code which needs to be excecuted at the end of each loop from the outer FOR###
)
您能否安排这样的代码(根据需要将参数传递给子例程):
FOR /f "tokens=1 delims=" %%G in (%pathToFile%) do (
CALL :YourSubr "Parm1" "Parm2"
### some Code which needs to be excecuted at the end of each loop from the outer FOR###
)
GOTO :eof
:YourSubr
REM 1=???
REM 2=???
set "dataRow=%%~G"
FOR %%I in (%searchValue%) do (
IF not "!dataRow:%%I=!"=="!dataRow!" (
### some Code here ### using passed parameters
)
)
我看到您正在使用延迟扩展,但是您的代码未显示SETLOCAL ...,因此请注意放置ENDLOCAL的位置。
此解决方案简单有效:
FOR /f "tokens=1 delims=" %%G in (%pathToFile%) do (
set "dataRow=%%~G"
set "break="
FOR %%I in (%searchValue%) do if not defined break (
IF not "!dataRow:%%I=!"=="!dataRow!" (
### some Code here ###
set "break=1"
)
)
###some similiar blocks like the one above which can also be ignored if one match was found beforehand###
### some Code which needs to be excecuted at the end of each loop from the outer FOR###
)