Windows批处理用于处理string的文件

我有以下代码来确定一个文件是否在第二行(:: echo1,:: echo2和:: echo3)上有一个string。 然而,当我在子例程中运行我的代码时:IF命令中的_verify在FOR命令中完全跳过了IF的else节,增加了variables%chk%并返回到:_verify,这又增加了数量of :: echo,它正在文件中查找并再次search同一个文件(它需要search每个文件的全部3个)。 我尝试将IF命令反转为IF NOT,并将其他命令切换到前面,然后调用:_redeem last,但同样的错误发生了。 (注意:它完成了包含:: echo1的文件的正确调用:_verify)。 它只检查每个文件的:: echo1,因为它不会增加%chk%并转到:_verify了。 相反,它直接转到goto:eof并返回:标识find另一个文件,它将再次只处理:: 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 (ie 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 

相反,它直接转到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 )