如何检查批处理是否成功执行过程

时间:2014-11-21 23:57:14

标签: batch-file cmd batch-processing command-prompt

我尝试创建一个安装nodeJs,express和bower的Windows批处理脚本,但是现在不起作用,我不知道是什么问题,因为终端在几秒钟后关闭了。 如果我将批处理脚本的所有指令逐个运行到CMD中,那么一切正常。 如何验证每个进程是否成功执行?我的意思是如果安装快递失败,请收到"fail express framework install"之类的消息 我认为问题是脚本在尝试安装下一个块时不等待完成安装fisrt代码块。 有什么想法解决?
这是我的剧本:


::Windows Batch Script for Install nodeJs, express framework for nodeJs, bower and Ionic

@setlocal
@echo off
:: Install nodeJs & NPM
echo Instalando NodeJs ^& NPM (Node Package Manager)...
echo.
start /wait msiexec /i C:\EMWA\installers\node.msi /passive /norestart

::Install express framework
echo Instalando express framework nodejs
echo.
npm install express-generator -g

:: Install bower
echo Install bower
echo.
npm install -g bower

:: create a test project
IF NOT EXIST C:\EMWA\projects 
    mkdir C:\EMWA\projects
IF NOT EXIST C:\EMWA\projects\emwa_test
    mkdir C:\EMWA\projects\emwa_test

pause > null

1 个答案:

答案 0 :(得分:0)

这是ERRORLEVEL语句中IF关键字的用途。有关详细信息,请参阅IF /?

C:\...>if /?
Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
....

  NOT               Specifies that Windows should carry out
                    the command only if the condition is false.

  ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code equal to or greater than the number
                    specified.
....
%ERRORLEVEL% will expand into a string representation of
the current value of ERRORLEVEL, provided that there is not already
an environment variable with the name ERRORLEVEL, in which case you
will get its value instead.  After running a program, the following
illustrates ERRORLEVEL use:

    goto answer%ERRORLEVEL%
    :answer0
    echo Program had return code 0
    :answer1
    echo Program had return code 1

You can also use numerical comparisons above:

    IF %ERRORLEVEL% LEQ 1 goto okay
....
C:\...>

只要命令设置退出状态,许多人都会这样做。

相关问题