The PAUSE Command: Freezing TIME

Say, you  create a batch file which shows the directory listing of a particular folder (DIR) before performing some other task. OR sometimes before deleting all files of folder, you need to give the user time to react and change his mind .PAUSH, the name says it all is used to time out actions of a script, Consider the following scenario:

 REM This batch program deletes *.doc files in ther current folder.

 REM But it gives the user to react and abort this process.

@ECHO OFF

ECHO warning: Going to delete all Microsoft Word  documents.

ECHO Press CTRL+ C to abort or simply press a key to continue.

PAUSE

Del *.doc

 Now, when you execute this batch program, you get the following output:

C:\WINDOWS >a.bat

WARNING: Going to delete all Microsoft Word documents.

Press CTRL+ C abort or simply press a key to continue.

Press any key to continue.............

 

The bath file program actually asks the user if he wishes to continue and the batch file program (CTRL + C and CTRL + Break bring about the same results)

 

^ C

Terminate batch job (Y/N)? y

After this you will get the DOS Prompt back.

 

Parameters: Giving Information to batch programs

 

To make batch programs really intelligent, you need to be able to provided them with parameters which are nothing but additional valuable information which is needed to ensure that the batch program can work efficiently and flexibly. To understand how parameters work, look at the following script: 

@ECHO OFF

ECHO First parameters is % 1

ECHO Second parameters is %2

ECHO Third parameter is % 3

 This script seems to be echoing (printing ) messages on the screen, but what do the strange symbols %1, %2 etc., stand for? To find out what they stand for , save the above script and go to the DOS and execute this script by passing the parameters below:

C:\ winsows > batch_ file _ name abc def ghi 

This batch file produces the following result: 

C:\ Windows> batch _file_ name abc def ghi

First Parameter is abc

Second parameter is def

Third Parameters is ghi 

The Firs line in the output is produced by the code line:

 ECHO First parameters is %1 

Basically, what happens is that when DOS encounters the %1 symbol, it examines the original command used to execute the program and looks for the first word (argument) after the batch filename and then assigns %1 to the value of  that word. So ,one can say that in the ECHO statement %1 is replaced with the value of the first argument . In the above example, the first word after the batch file name is abc, therefore, %1 is assigned its value.

The %2 symbol too works in the similar way, the only difference being that instead of the first argument, DOS assigns it the value of the second argument, def, Now, all this symbols, %1 , %2 are called replaceable parameters.Actually, what happens is that %1 is not assigned the value of the first argument, but in fact, it is replaced by the value of  the first argument.

 

If the batch file command has more parameters then what the batch file is looking for , then the extras are ignored, for example, if while executing a batch file program, we pass four arguments, but the batch file program requires only 3 parameters, then the fourth parameters is ignored.

 

To understand the practical usage of parameters, let’s take up a real life example, Now, the following script requires the user to enter the name of the files to be deleted and the folder in which they are located.

 

@ECHO OFFD

CD\

CD\ %1

DEL %2

 

This script can be called from the DOS prompt in the following way:

 

C:\ windows> batch_ file_ name windows\ temp *.temp

 

In a single script we cannot use more that nine replicable parameters. This means that a particular batch file will have replaceable parameters, the % 0 parameter, that contains the name of the batch file itself.

Hosted by www.Geocities.ws

1