Skip to main content

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% :: This command will show the value of summation which is stored in the variable sum. echo on :: After this command every command will be echoed on the screen. If you want to pass a filename as a parameter and work on that file then some additional references will be needed. The following Table will help you to know about them.
ParametersComments
%~f1Expands %1 to a fully qualified pathname. If you passed only a file name from the current directory, this parameter would also expand to the drive or directory.
%~d1Extracts the drive letter from %1
%~p1Extracts the path from %1
%~n1Extracts the filename from %1 without the extension
%~x1Extract the file extension from %1
%~s1Changes the n and x options’ meanings to reference the short name. You would therefore use %~sn1 for the short filename and %~sx1 for the short extension.
And we also combine them to get our expected result. As Example.... %~dp1 to expand %1 to a drive letter and path only or use %~sp1 to display short path.

Comments

Popular posts from this blog

Class Point:: It holds co-ordinates of a point in format (x,y)

A class Named "Point" which stores the Co-ordinates of a point in (x,y) Format. This Class Contains:: O Constructor with default arguments O Copy Constructor O Function to display which Co-ordinate a Point belongs to O Function to get value of x of a point O Function to get value of y of a point O Function to get the Radious Vector (r) of a point O Function to get the Theta of a point O Overloaded Operator - to find out distance between two points O Overloaded Operator >> to take input O Overloaded operator O Destructor #include<iostream.h> #include<math.h> class point{ //Data Members int x,y; //They hold the value of X and Y co-ordinate of a Point. public: //Member Functions point(int t1=0, int t2=0) //Constructor with default arguments. { x=t1; y=t2; } point(point &t) //Copy Constructor { x=t.x; y=t.y; } int coordinate(void); //It returns the point's Co-ordinate, e.g., 1, 2, 3, or 4th. int getx(...

0s and 1s Count in 1 Byte Memory Word

This program will count all the 1s and 0s individualy presents in a 1 byte word. And store the result in individual places in Memory. Suppose, argument byte is stored in memory location 2050H and store the result of 1's count at 2060H memory location and 0's at 2061H Memory Location Hex Code Label Mnemonics Comment 3050 3051 3052 3A 50 20 START: LDA 2050H ;Load Accumulator with the number stored at location 2050H 3053 3054 06 00 MVI B, 00H ;Initiate 0's Counter 3055 3056 0E 00 MVI C, 00H ;Initiate 1's Counter 3057 3058 16 08 MVI D, 08H ;Initiate Bit Counter 3059 07 LOOP: RLC ;Rotate Accumulator Left with Carry 305A 305B 305C DA 61 30 JC ONE ;If 1 is found count 1 305D 04 INR B ;If 1 is not found it must be 0. So increase 0's Counter 305E 305F 3060 D2 62 30 JNC NEXT ;Go to next Bit 3061 0C ONE: INR C ;Increase 1's Counter 3062 15 NEXT: DCR D ;Decrease the Bit Counter 3063 3064 3065 C2 ...