Exiting batch with `EXIT /B X` where X>=1 acts as if command completed successfully when using && or || operators between batch calls

前端 未结 4 740
借酒劲吻你
借酒劲吻你 2020-12-09 04:26

I\'m trying to chain a series of .bat files using the EXIT /B X command to return success or failure and && and || for conditi

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 04:55

    If you use start /wait you can also use this in a very simple Windows application (written in C#) called by DOS batch files like so:

    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Environment.ExitCode = Convert.ToInt32(args[0]);
        }
    }
    

    Then the application can be called by your DOS batch file and evaluate the result. i.e.

    c:> start /wait SetRC 1
    c:> if "%errorlevel%"=="1" goto abort
    

    NOTE: the /wait is not necessary in a batch file.

    You could pass in the return code you want as an argument to your program.cs and get it out this way guaranteed.

提交回复
热议问题