Errorhandling in dos .bat files
Moderator: DosItHelp
-
- Posts: 4
- Joined: 21 May 2009 05:18
Errorhandling in dos .bat files
Is there any error handling mechanism in the .bat files?
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
The variable %errorlevel% is always set to the error level or exit code of each command. In general, 0 is success, and anything else is some type of failure. Some commands have multiple exit codes representing different types of errors.
You can use %errorlevel% with if statements in a bat file to add error checking and workarounds.
Is that what you want?
You can use %errorlevel% with if statements in a bat file to add error checking and workarounds.
Is that what you want?
Re: Errorhandling in dos .bat files
basically just place errorlevel strings where you feel you are likely to have errors and move them around to narrow down the potential causes. Microsofts website lists over 16,000 errorcodes!
will echo an error code
will inact error resolution1 if that fails error resolution2
will start a program if that fails exit the current program
will output error codes to a log file
i do have a similar question, and that is how do i make an error catcher shell. ?? i think i would need a script meant to catch errors that can run other scripts?
will echo an error code
Code: Select all
IF ERRORLEVEL 1 ECHO %ERRORLEVEL%
Code: Select all
IF ERRORLEVEL 1 GOTO ERRORHANDEL1 || GOTO ERRORHANDEL2
Code: Select all
IF ERRORLEVEL 1 START || EXIT
Code: Select all
IF /I %errorlevel% gtr 0 echo %errorlevel% > %systemdrive%\temp\errorlog.txt
i do have a similar question, and that is how do i make an error catcher shell. ?? i think i would need a script meant to catch errors that can run other scripts?
Re: Errorhandling in dos .bat files
Your above examples B and C are completely incorrect syntax, and D is exceptionally poor syntaxJedininja wrote: ↑14 Jan 2022 06:29basically just place errorlevel strings where you feel you are likely to have errors and move them around to narrow down the potential causes. Microsofts website lists over 16,000 errorcodes!
will echo an error codewill inact error resolution1 if that fails error resolution2Code: Select all
IF ERRORLEVEL 1 ECHO %ERRORLEVEL%
will start a program if that fails exit the current programCode: Select all
IF ERRORLEVEL 1 GOTO ERRORHANDEL1 || GOTO ERRORHANDEL2
will output error codes to a log fileCode: Select all
IF ERRORLEVEL 1 START || EXIT
Code: Select all
IF /I %errorlevel% gtr 0 echo %errorlevel% > %systemdrive%\temp\errorlog.txt
i do have a similar question, and that is how do i make an error catcher shell. ?? i think i would need a script meant to catch errors that can run other scripts?
To prevent unintended whitespace duiring output or numeric values being mistaken for the file handle during redirection:
1>>"%TEMP%\%~n0_ERR.log" (Echo(%Errorlevel%)
"||" is not the same as else. Correct If syntax for strings:
Code: Select all
If "%Variable%" == "Value" (
Rem command
)Else (
Rem Other Commands
)
The below example script uses "||" as a way of performing AND logic to restrict input to within a defined range by using Set /A to perform a series of sums desgnied to provoke a divide by zero errer if a value doesnt meet the defined criteria.
If no error is triggered , the command has met all the And conditions the sum tested.
Code: Select all
::# :ValidNum Function for accepting input of a positive number, with arguments to declare
::# return var, Maximum and Minimum ranges. Scripted to be Proof against code injection.
::# Blocks entry of: non integer strings, negative numbers, hex, octal, numbers with leading 0's and 0 literal,
::# and Integers outside of the declared range.
@Echo off
Rem usage examples:
Rem Example 1 - Return var $RV, Max 31, default Min: 1
Call:ValidNum $RV 31
Rem Example 2 - Return var $RV2, Max 20, Min: 10
Call:ValidNum $RV2 20 10
Rem Example 3 - Max default: 2147483647, Min 5, return var default: $Num
Call:ValidNum "" "" 5
Set $
Goto:Eof
:ValidNum [returnvar] [max] [min]
Rem - Forces positive numeric input within min max range.
SETLOCAL
Set "sign=-1"
:VNumIn
%= ensure nul value =%
Set "input="
%= define min value Arg3 ; else min integer =%
2> nul Set /a "min=%~3","1/min","1/(sign-(min>>31))","1/(%~2/%~3)" || Set "min=1"
%= define max value Arg2 ; else max integer =%
2> nul Set /a "max=%~2","1/max","1/(sign-(max>>31))","1/(%~2/min)" || Set "max=2147483647"
%= Prompt for input =%
Set /p "input=Enter a number GEQ %min% LEQ %max%: "
%= Any Input surviving the Below Set /A testing is safe for expansion =%
%= Input Testing. input +/- , input > min , input < max , Hex/Octal for comparison =%
2>nul Set /a "1/(sign-(input>>31))","max/(input/min)","1/(max/input)","HexOct=input" || Goto:VNumIn
%= compare assignments to block input of hex, octal or numbers with leading 0's =%
If not "%Input%"=="%HexOct%" Goto:VNumIn
( %= return value in Arg1 if used ; else return in $Num =%
ENDLOCAL & Set "%~1=%input%" 2> nul || Set "$Num=%input%"
Goto:Eof
)
OR is most easily implemented with Findstr in conjunction with "&&" Note: each space seperated string in the search patterns is an individual search pattern
Code: Select all
@Echo off
Echo(%ComputerName%|%SystemRoot%\System32\findstr.exe /li "a c e f h k" > nul && (
Echo(%Computername contains one or more instances of: a c e f h or k
) || (
Echo(%Computername contains no instances of: a c e f h or k
)
[Code]