只有批处理脚本才能执行语句

时间:2014-10-04 15:11:02

标签: batch-file scripting

我有以下批处理脚本

@echo off
cd %USERPROFILE%\desktop

IF EXIST output.txt (
   FOR /F "tokens=*" %%G IN (output.txt) DO (
     echo %%G
     IF "%%G" == "BUILD FAILED" (
       echo failed
       pause
       exit (1)
     )
     IF "%%G" == "BUILD SUCCESS" (
       echo success
       pause
       exit (0)
     )
  )
)
pause

它从具有一行(BUILD SUCCESS或BUILD FAILED)的文本文件中读取。我确定的是,如果首先列出BUILD SUCCESS if语句并且BUILD SUCCESS在文件中,它将按预期退出。但是,如果BUILD SUCCESS在文件中并且BUILD SUCCESS if语句是第二个(如代码示例中所示),它从不捕获它并使其进入文件末尾的暂停。我的语法突出显示是用括号做了奇怪的事情,所以我认为它与我如何分组的东西有关,但我无法弄明白。

3 个答案:

答案 0 :(得分:1)

删除exit命令中的括号。

答案 1 :(得分:1)

这个脚本非常适合我。我希望它能为您提供解决问题所需的建议:

@ECHO off
ECHO Start of script
IF NOT DEFINED T set T=Undefined Title
TITLE %T%
::cd %USERPROFILE%\desktop
IF NOT EXIST output.txt (
  ECHO Generating sample file...
  ECHO BUILD SUCCESS>output.txt
  ECHO BUILD FAILED>>output.txt
)

IF EXIST output.txt (
   FOR /F "tokens=*" %%G IN (output.txt) DO (
     echo %%G
     IF "%%G" == "BUILD FAILED" (
       echo failed
       GOTO :ERR
     )
     IF "%%G" == "BUILD SUCCESS" (
       echo success
     )
  )
)
GOTO :END
:ERR
ECHO Failed.  Will close in 10 seconds...
FOR /l %%a in (10,-1,1) do (TITLE %T% -- closing in %%as&ping -n 2 -w 1000 127.1>NUL)
EXIT 1
:END
ECHO End of script.  Will close in 10 seconds...
FOR /l %%a in (10,-1,1) do (TITLE %T% -- closing in %%as&ping -n 2 -w 1000 127.1>NUL)
EXIT 0

答案 2 :(得分:1)

这是一个替代脚本:

@echo off
find "BUILD SUCCESS" < "%USERPROFILE%\desktop\output.txt" >nul && (echo success & pause & exit 0)
find "BUILD FAILED"  < "%USERPROFILE%\desktop\output.txt" >nul && (echo failed  & pause & exit 1)