Skip to main content

Posts

Showing posts from February, 2010

Passing Parameter to a Dos/Windows Batch File

Some time we need to provide the parameters ( the data to be processed ) to a batch file at the time of calling the batch process. Suppose we want to add two numbers by a batch file. So, let's consider a name for the batch file, say, add.bat Now, if we want to add 4 and 5 by this batch file we have to write on the command prompt as, add 4 5 Then it will show the result 9 In a batch file the parameters are referred by numbering like, for the discussed batch file %1 refers the first value of parameter, i.e., 4 and %2 refers 5 . So the content of the batch file will be.... @echo off set/a sum= %1 + %2 echo %sum% echo on Comments on each line............... @echo off :: This command will off the echoing (displaying) the next commands until the echoing is on. set/a sum= %1 + %2 :: This Command evaluate the addition process and store the value in a variable named sum . /a is a switch for set command to evaluate any expression. echo %sum%