Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 仅当执行语句时才批处理脚本_Batch File_Scripting - Fatal编程技术网

Batch file 仅当执行语句时才批处理脚本

Batch file 仅当执行语句时才批处理脚本,batch-file,scripting,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 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

它从只有一行的文本文件中读取,要么生成成功,要么生成失败。我确定的是,如果buildsuccessif语句列在第一位,并且buildsuccess在文件中,那么它将按预期退出。但是,如果buildsuccess在文件中,buildsuccess if语句是第二个语句(如代码示例中所示),它将永远不会捕获它,并使其在文件末尾暂停。我的语法突出显示使用括号做了一些奇怪的事情,因此我假设这与我如何分组有关,但我无法理解。

退出
命令中删除括号

删除
exit
命令中的括号

删除
exit
命令中的括号

删除
exit
命令中的括号

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

@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

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

@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

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

@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

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

@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

下面是另一个脚本:

@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)
@echo关闭
查找“生成成功”<%USERPROFILE%\desktop\output.txt“>nul&&(回显成功&暂停&退出0)
查找“生成失败”<%USERPROFILE%\desktop\output.txt“>nul&&(回显失败&暂停&退出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)
@echo关闭
查找“生成成功”<%USERPROFILE%\desktop\output.txt“>nul&&(回显成功&暂停&退出0)
查找“生成失败”<%USERPROFILE%\desktop\output.txt“>nul&&(回显失败&暂停&退出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)
@echo关闭
查找“生成成功”<%USERPROFILE%\desktop\output.txt“>nul&&(回显成功&暂停&退出0)
查找“生成失败”<%USERPROFILE%\desktop\output.txt“>nul&&(回显失败&暂停&退出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)
@echo关闭
查找“生成成功”<%USERPROFILE%\desktop\output.txt“>nul&&(回显成功&暂停&退出0)
查找“生成失败”<%USERPROFILE%\desktop\output.txt“>nul&&(回显失败&暂停&退出1)