首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于处理字符串文件的Windows批处理

用于处理字符串文件的Windows批处理
EN

Stack Overflow用户
提问于 2011-08-09 09:52:09
回答 1查看 422关注 0票数 1

我使用以下代码来确定文件的第二行是否有字符串(::echo1、::echo3 2和::echo3 3)。但是,当我出于某种原因在for命令中IF命令的子例程:_verify中运行代码时,它会完全跳过IF的else部分,这会增加变量%chk%并返回到:_verify,这反过来会增加它在文件中查找的::echo的数量,并再次搜索同一个文件(它需要在每个文件中搜索所有3个)。我尝试将IF命令反转为IF,并将else切换为最前面,将call :_redeem放在最后,但同样的错误也发生了。(注意:对于包含::Echo1的文件,它确实正确地完成了:_verify调用)。它只检查每个文件的::eco1,因为它不会增加%chk%并再次转到:_verify。相反,它直接转到goto :eof并返回到:identify来查找另一个文件,它将再次查找该文件,只处理::echo1。我添加了一些注释来帮助解释我的脚本。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-09 15:36:19

而不是

,它直接转到goto :eof

情况就是这样,因为您编写代码就是为了这样做。

你想写

代码语言:javascript
复制
if %chk%==4 (call :_reserve & goto :eof)

但是你的代码是这样工作的

代码语言:javascript
复制
if %chk%==4 call :_reserve
goto :eof

你应该避免使用&分隔符,最好使用带括号的多行。

代码语言:javascript
复制
for %%A in (tmp*.tmp) do (
   set cap%gt1%=%%A
   call :_verify 
)
....
if %chk%==4 (
   call :_reserve
   goto :eof
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6990608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档