Wednesday, October 28, 2009

Errorlevel: Checking for Errors while Executing a Batch Script

Sometimes it's necessary to check for errors while executing a batch script. Command interpreter has built-in support for storing the return code of the command or program most recently executed. Note: error level is not a variable from the point of a batch script writer. You can't check it with the following code:
If %Errorlevel%==1 goto Error
You should write instead this line:
If Errorlevel 1 goto Error
You can't check whether error level equals to zero or other defined value. "If errorlevel" executes the command following this statement if error level is equal or greater than the specified value. This strange syntax is very handy because usually you don't have to check the error level for any value except zero. So using this construction you avoid checking for a large amount of different values.
Though you can use alternative syntax that also works:
If %Errorlevel% NEQ 0 goto Error
Here you can see that error level is used like a variable. But it's a fake variable. You can't use it in any other way except the way shown above. Note: this code works only under Windows NT family operating systems (NT 4.0, 2000, XP, etc.). So if you need to write MS-DOS-compatible batch file, you have to use "If errorlevel" command.

Tuesday, October 20, 2009

Pauses and Delays in Batch Scripts

In some cases you need to wait for a certain time after calling an application from your batch file. For example, it's useful when this application must finish its work. How can one include delay in his batch script? There are several different solutions.
  1. The first way is the simplest one. You can just include "pause" command in your script. This way has a really big drawback: this command requires a key pressing to continue the execution. So this command cannot be used in pure automated scripts, because you must ask someone to press a key.
  2. The second way is to use ping command. Of course, this command is not designed for adding delays to the batch files, but it's really handy to use it in this case. The following code will make delay for ten seconds: ping -n 11 -w 1000 127.0.0.1 > nul
  3. You can use 'Sleep' utility from Windows Resource Kit. Its usage will look like sleep 10.
I'd better use the second variant because it's quite simple and will run on all versions of Windows without any external tools.

Friday, October 16, 2009

Checking for Existence of Variables and Parameters

Sometimes it's necessary to check in a batch file whether a certain variable is assigned. For example, it's useful when you need to check for some condition in "procedure" under certain label from main batch script. The usual way to check for existence looks like this:
if defined %VARIABLE% echo Variable exists!
But if you try to perform the same trick with parameters given to your batch script while its execution, you will be surprised. Code like
if defined %3 echo Parameter defined!
will not work correctly.
So what should one do to check whether certain parameter is passed to the batch file? The perfect solution is to compare the value of this parameter to empty string:
if "%1"=="" echo Help on parameters...
It's necessary to perform such check for existence of certain parameters if they are expected by your batch file. You should show short summary about the parameters of your batch file if there are less parameters than you expected. This simple technique will make your script more friendly. Even if one day you forget what parameters you need pass to it, you wouldn't read its code once more to remember them.

Monday, October 12, 2009

Working on Dr.Batcher 2.0 Beta

The first post on this blog is going to be about working on the next release of Dr.Batcher. Ok, it's a good topic.
The most interesting and valuable feature of Dr.Batcher 2.0 must be Simple mode of the script editor. What does this mean? Imagine that you speak to computer in English while writing a batch script. So you write "Delete file" instead of "Del", "Register library" instead of "regsvr32.exe", etc. You have no need to remember all the keys of each command, because the editor con tell you everything you need to know on each parameter.
This will look somehow like this:
It's not hard to develop such tool for the new batch scripts creation. But if you develop pure editor that allows you to edit the existing batch files, you should create a batch script parser. The parser is the most difficult component of Dr.Batcher to develop, and the major part of current beta-testing work is being performed to make the parser work right with any kind of batch files. But it's needful because users want to edit their old scripts and update them. We believe that our work on parser won't be a waste of time.

Translate