我有以下代码来确定文件是否在第二行(:: echo1,:: echo2和:: echo3)上有一个字符串。但是,当我在子程序中出于某种原因运行我的代码时:在FOR命令内的IF命令中_verify它完全跳过IF的else部分,它增加了变量%chk%并返回到:_verify,这又增加了数字of ::它正在文件中查找并再次搜索同一个文件(它需要搜索所有3个文件的每个文件)。我尝试将IF命令反转为IF NOT并将else切换到前面并调用:_redeem last但发生了同样的错误。 (注意:它完成了调用:对包含:: echo1的文件正确_verify)。它只检查:: echo1的每个文件,因为它不会增加%chk%并再次转到:_verify。相反,它直接转到goto:eof并返回:识别以找到它将再次发送的另一个文件,仅处理:: echo1。我添加了评论以帮助解释我的脚本。
set gt1=1
setLocal EnableDelayedExpansion
::Identifies all files meeting the criteria (name being tmp*.tmp) and sets cap%gt1% equal the filename. Also checks to see if there are no files left (if the filename doesn't exist (i.e. it's blank because there are none left)).
:identify
set chk=1
if %gt1%==4 goto :restore
for %%A in (tmp*.tmp) do (set cap%gt1%=%%A) & call :_verify
if not exist !cap%gt1%! goto :error
goto :identify
:_verify
::Verifies that the specific file it's looking at (set as cap%gt1% in :identify) has the string ::echo1, 2 or 3 as the second line.
if %chk%==4 call :_reserve & goto :eof
for /f "skip=1" %%B in (!cap%gt1%!) do if %%B==::echo%chk% (call :_redeem) else (set /a chk=%chk%+1) & (goto :_verify)
goto :eof
:_redeem
::Renames files that are confirmed to have the string to their string name (minus the ::).
ren !cap%gt1%! echo%chk%.tmp
set /a gt1=%gt1%+1
goto :eof
:_reserve
::Subroutine used to temporairly discard files that do not meet the requirements so they will not be processed again in :identify during loopback.
if not exist temp50 mkdir temp50
move !cap%gt1%! temp50
goto :eof
:restore
::Restores files that were put in :_reserve to their previous location.
if exist %~dp0\temp50 cd temp50 & for %%C in (tmp*.tmp) do move %%C %~dp0 & cd .. & rmdir temp50
pause
:error
::Error in case it can't find all three files containing the strings.
echo Unable to find program files. Please reinstall.
echo.
pause
quit
答案 0 :(得分:1)
相反,它直接转到goto:eof
在您编写代码时就是这种情况 你想写
if %chk%==4 (call :_reserve & goto :eof)
但是你的代码可以作为
if %chk%==4 call :_reserve
goto :eof
您应该避免使用&
分隔符,最好使用带括号的多行。
for %%A in (tmp*.tmp) do (
set cap%gt1%=%%A
call :_verify
)
....
if %chk%==4 (
call :_reserve
goto :eof
)