Codessentials

  • Increase font size
  • Default font size
  • Decrease font size
Home Coding tips Scripts Dos Scripts Check errorlevel

Check errorlevel

The Microsoft MS-DOS "IF ERRORLEVEL <number>" statement checks for error levels of the given number or greater. If you want to check for a specific error level, you should use the following command construct
   IF ERRORLEVEL <N> IF NOT ERRORLEVEL <N+1> <COMMAND>
where <N> is the desired number. The <N+1> portion of the command must be calculated as the command is entered, because the MS-DOS command interpreter cannot perform mathematical calculations.

Examples

The following example executes command1 (whatever that may be) and if this command returns an exitcode 1 or greater then the dos script exits immediatly.
Only if command1 returns 0, the script continous and executes command2.
@echo off 
command1
if errorlevel 1 goto finally
command2
:finally
echo exit %errorlevel%
exit %errorlevel%

The following example executes command1 and if this command returns an exitcode 1 then the dos script exits immediatly.
If command1 returns 0 or a value greater than 1 the script continous and executes command2.

@echo off 
command1
if errorlevel 1 if not errorlevel 2 goto finally
command2
:finally
echo exit %errorlevel%
exit %errorlevel%
 

Bookmark

AddThis Social Bookmark Button


Newsflash

Jack Wallen is the host of the TechRepublic open source blog as well as a regular writer in the "10 Things..." and the "How Do I..." blogs.

In this article Jack Wallen reviews Yadis! Backup.

Some highlights:

There are tons of backup solutions available, ranging from simple, single-user applications to much larger, complex solutions. Most all of these solutions will do what the creators claim — back up your data. You always hope that data is backed up safely and that it will be there in that unforeseen moment when you need it. There is one backup solution amid the myriad of others that offers a unique little twist on the old tried-and-true process. Yadis! Backup.
...

Yadis! Backup is one of the more foolproof backup solutions I’ve come across. If you are looking for a simple backup solution that does not require any scheduling, this might be what you are seeking. It’s not exactly Enterprise-ready, but for smaller backups it’s a pretty sweet solution.

Read the full article here.