Thursday, January 10, 2013

What is errorlevel?



 Sooner or later every batch files creator faces the problem of checking the result of one command execution before executing the other one. For that we have built-in variable “errorlevel” that changes its value after each command execution. Its value can vary for different commands used in batch files. Usually successful execution of the command is displayed as zero value of variable, but not always other value implies any errors. So if you create batch file, you should remember about this meaning of errorlevel.
This variable is used as follows:
if errorlevel 0 echo Errorlevel is 0 or greater
Instead of “echo Errorlevel is 0 or greater” you can put any necessary command. After “if” you can add “not” (no quotation marks for this time again) – then the value becomes diametrically opposed. It should be mentioned that “errorlevel N”-construction does not mean precise equality of variable “errorlevel” to value N. It means that the command will be executed only in case “errorlevel” value is or is greater than N. Thus it is more convenient to check errors with the help of “if errorlevel” –construction than to execute the command successfully. But if you write “if not errorlevel 1”, that means in case “errorlevel” is nonnegative, we obtain the result of “errorlevel” equal to zero. Unfortunately nonnegative values of this variable are not guaranteed. For more details you can read about “if errorlevel” –construction in Windows help manual.

Saturday, January 5, 2013

Why Is There No Percentage Mark in the Line Seen on the Screen?



In batch files percentage mark is used to indicate variables, or variable environments, or internal variables of the BAT-file. So, if you’d like to see percentage mark on the screen, you just need to use it twice: for example, “echo This day performed 0.25%% of the whole project”. By the way, is not the only symbol that has a special meaning in BAT files. Such symbols also include “>”, “<”, etc. They can be displayed on the screen with the help of combinations “^<”, “^>” (i.e. you put a tick before the symbol).

Translate