| 1. |
Solve : Do you really need EXIT at the end of a BAT file?? |
|
Answer» I leave off EXIT, and the BAT file works fine. I don't notice any problems. Thanks! When do you ever want to set an "exit code"? . . . Also, if the .BAT file ran, why would there be an error? Don't think of it as always an indication of an "error". Sometimes it is just an "exit code". Historically, going right back to MS-DOS in the Microsoft world, programs and scripts have been able to leave behind a code, that is a number, in memory, which another script or program can read. It was called "ERRORLEVEL" and the name has stuck. Here are two simple EXAMPLE scripts. Put them both in the same folder and run test1.bat (1) test1.bat Code: [Select]echo off echo Running test2.bat... call test2.bat echo Back from test2.bat if %errorlevel% equ 255 echo Answer was "yes" if %errorlevel% equ 128 echo Answer was "no" pause (2) test2.bat Code: [Select]echo off :loop set /p a="Are you happy (answer y or n)? " if /i "%a%"=="y" exit /B 255 if /i "%a%"=="n" exit /B 128 echo Answer y or n! goto loop You can catch errorlevels returned by other languages: Put these in the same folder as each other and run test3.bat (1) stringlen.vbs Code: [Select]wscript.quit len(wscript.arguments(0)) (2) test3.bat Code: [Select]echo off :loop set /p str="Enter a string: " if "%str%"=="" echo String cannot be empty & goto loop wscript stringlen.vbs %str% echo String has %errorlevel% character(s) pause This would be better... copes when string has spaces... (2) test3.bat Code: [Select]echo off :loop set /p str="Enter a string: " if "%str%"=="" echo String cannot be empty & goto loop wscript stringlen.vbs "%str%" echo String has %errorlevel% character(s) pause |
|