Batch Files - Error Handling

后端 未结 7 1676
谎友^
谎友^ 2020-11-28 08:54

I\'m currently writing my first batch file for deploying an asp.net solution. I\'ve been Googling a bit for a general error handling approach and can\'t find anything really

相关标签:
7条回答
  • 2020-11-28 09:32

    Its extremely easy! Create a file that contains:

    call <filename>  // the file you made
    cls
    echo An error occured!
    <Your commands>
    pause
    

    So now when you start it, it will launch your program as normal. But when anything goes wrong it exits and continues the script inside the first file. Now there you can put your own commands in.

    0 讨论(0)
  • 2020-11-28 09:36

    I generally find the conditional command concatenation operators much more convenient than ERRORLEVEL.

    yourCommand && (
      echo yourCommand was successful
    ) || (
      echo yourCommand failed
    )
    

    There is one complication you should be aware of. The error branch will fire if the last command in the success branch raises an error.

    yourCommand && (
      someCommandThatMayFail
    ) || (
      echo This will fire if yourCommand or someCommandThatMayFail raises an error
    )
    

    The fix is to insert a harmless command that is guaranteed to succeed at the end of the success branch. I like to use (call ), which does nothing except set the ERRORLEVEL to 0. There is a corollary (call) that does nothing except set the ERRORLEVEL to 1.

    yourCommand && (
      someCommandThatMayFail
      (call )
    ) || (
      echo This can only fire if yourCommand raises an error
    )
    

    See Foolproof way to check for nonzero (error) return code in windows batch file for examples of the intricacies needed when using ERRORLEVEL to detect errors.

    0 讨论(0)
  • 2020-11-28 09:40

    A successful ping on your local network can be trapped using ERRORLEVEL.

    @ECHO OFF
    PING 10.0.0.123
    IF ERRORLEVEL 1 GOTO NOT-THERE
    ECHO IP ADDRESS EXISTS
    PAUSE
    EXIT
    :NOT-THERE
    ECHO IP ADDRESS NOT NOT EXIST
    PAUSE
    EXIT
    
    0 讨论(0)
  • 2020-11-28 09:41

    I guess this feature was added since the OP but for future reference errors that would output in the command window can be redirected to a file independent of the standard output

    command 1> file - Write the standard output of command to file

    command 2> file - Write the standard error of command to file

    0 讨论(0)
  • 2020-11-28 09:47

    Python Unittest, Bat process Error Codes:

    if __name__ == "__main__":
       test_suite = unittest.TestSuite()
       test_suite.addTest(RunTestCases("test_aggregationCount_001"))
       runner = unittest.TextTestRunner()
       result = runner.run(test_suite)
       # result = unittest.TextTestRunner().run(test_suite)
       if result.wasSuccessful():
           print("############### Test Successful! ###############")
           sys.exit(1)
       else:
           print("############### Test Failed! ###############")
           sys.exit()
    

    Bat codes:

    @echo off
    for /l %%a in (1,1,2) do (
    testcase_test.py && (
      echo Error found. Waiting here...
      pause
    ) || (
      echo This time of test is ok.
    )
    )
    
    0 讨论(0)
  • 2020-11-28 09:51

    Other than ERRORLEVEL, batch files have no error handling. You'd want to look at a more powerful scripting language. I've been moving code to PowerShell.

    The ability to easily use .Net assemblies and methods was one of the major reasons I started with PowerShell. The improved error handling was another. The fact that Microsoft is now requiring all of its server programs (Exchange, SQL Server etc) to be PowerShell drivable was pure icing on the cake.

    Right now, it looks like any time invested in learning and using PowerShell will be time well spent.

    0 讨论(0)
提交回复
热议问题