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

টুক করে বকখালি

বকখালি অবস্থান:  দেশ: ভারতবর্ষ রাজ্য: পশ্চিমবঙ্গ জেলা: দক্ষিণ ২৪ পরগনা কি কি দেখার ?                 বিশাল সমুদ্র..... আরে, Bay of Bengal.... পুরো দীঘার ছোট edition... বিস্তৃত সমুদ্র সৈকত..... বেশ ঘন ম্যানগ্রোভ অরণ্য... আর যেটা খুব ভালো লাগে সেটা হল.... এখানে দীঘার মত এত ভীরভাট্টা নেই.... বেশ নিরিবিলি..... যারা একাকী একটা দিন নিভৃতে কাটাতে চান তাঁদের জন্য ব্যাপক জায়গা। (বা দুজনে নিভৃতে কাটাতে চান....)                  একটা Crocodile Park আছে ওখানে.....পশ্চিমবঙ্গ বন দপ্তর দ্বারা নির্মিত..... Bus Stand এর কাছেই। ভালই সাজানো গোছানো.....২ টাকা করে Entry Fee.... ওর মধ্যে একটা পুকুর আছে, যার ঘাট বাঁধানো । কিছুক্ষণ ওখানে বসতেও খুব ভালই লাগে..... বেশ গাছের ছায়া.... মৃদু মন্দ শীতল হাওয়া..... উফ: প্রাণটা যেন জুড়িয়ে যায়..... যেন মনে হয় প্রকৃতির কোলে মাথা রেখে একটু শান্তিতে ঘুমিয়ে নিই.... একটা Tube Well ও আছে.....

Majay Byatha