Errorhandling in dos .bat files
Posted: 22 May 2009 03:10
Is there any error handling mechanism in the .bat files?
A Forum all about DOS Batch
https://www.dostips.com/forum/
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
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?
Code: Select all
If "%Variable%" == "Value" (
Rem command
)Else (
Rem Other Commands
)
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
)
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]