Question:
How do I pass parameters to a program inside a batch file?
many_happy_kittens
2009-11-13 13:21:12 UTC
I wrote a program in C++, and it asks for 2 values. It does some things, and I'm going to graph the results I get each time I run it. That's 10 data points for 5 graphs, and the program has to run for each point. That's why I'm writing a batch file.

How do I have the batch file give the program the values it needs to run? I'm new to writing batch files, and my searches have come up with nothing.
Nine answers:
?
2009-11-13 13:28:54 UTC
Look into getting the C++ program to accept the values through command line arguments as well as prompting (check for arguments, and if they're not there, then prompt). Then you can just pass the arguments to the program with the batch script.



Richard "Doctor DOS" Bonner has a good set of tutorials here: http://www.chebucto.ns.ca/~ak621/DOS/BatBasic.html , although you're probably beyond a lot of it.



--- Samuel
nieratko
2016-10-01 07:22:12 UTC
Batch File Arguments
?
2009-11-13 13:40:37 UTC
I have an idea that might just crazy enough to work.



Create a batch file that starts a loop and continue until the 5th loop has finished.

You can try to make the C++ program pickup its values from a text document.



So basically this would happen.

You start the batch file it ask you to enter 2 values, it writes that to a text file.

Excute the program in silent mode then delete the txt file.

When its finished it will ask you to input 2 values again and so it wil continue until the 5th loop has finished.



I can help with the batch programming but not the c++ part.
gelsey
2016-12-26 13:14:49 UTC
Bat File Parameters
anonymous
2009-11-14 13:58:27 UTC
Batch files Arguments (in my opinion) are the substring commands that you specify when you call the batch file. To show you in an understanding way take this for example:



In the command line you enter this command:

--------------------------

call bat1.bat Hello

--------------------------



And bat1.bat contained this code:



--------------------------

echo %1

pause

--------------------------



The outcome would output to the cmd screen:



--------------------------

Hello

Press any key to continue. . .

--------------------------



%1 is the first argument you specify that is placed after the filename. These arguments are separated by spaces (unless grouped together by double quotes). So with each argument specified the number increases to %2 and %3 and so on. %0 is the name of the batch file you are calling or starting.



The syntax works like this; compare:



--------------------------

%0 %1 %2

--------------------------

BatchFile1.BAT FirstArgument SecondArgument

--------------------------



Second Part - More Clarifying



This batch file will list the first two arguments and its own filename:



--------------------------

@echo off

echo.

echo The name of this file is %0

echo Argument 1: %1

echo Argument 2: %2

echo.

pause

--------------------------



From the command line or another batch file if you call this batch file with these arguments:



--------------------------

call BatchFile1.BAT Hello World

--------------------------



Then you should get an output like this (except for maybe the filename):



--------------------------

The name of this file is BatchFile1.BAT

Argument 1: Hello

Argument 2: World



Press any key to continue. . .

--------------------------



As you can see it got it's own filename(%0) and Hello was the first argument (%1) and World was the second argument (%2).



But what if we combined "Hello" and "World" into one argument using double quotes? Well if we did do that:



--------------------------

Call BatchFile1.BAT "Hello World"

--------------------------



Then it will treat that as one whole argument or %1 in this case and output this:



--------------------------

The name of this file is BatchFile1.BAT

Argument 1: "Hello World"

Argument 2:



Press any key to continue. . .

--------------------------



Since the second argument was not specified, then the value of %2 is Null; therefore the echo request of %2 echoes nothing to the screen.



Third Part - Drag and Drop



Now lets say you have two separate batch files on your desktop.



bat1.bat contained this code:



--------------------------

@echo off

echo %1

echo.

call %1

--------------------------



and bat2.bat contained this code:



--------------------------

@echo off

echo Hello World!

pause

--------------------------



If you would drag and drop bat2.bat into bat1.bat you would be opening bat1.bat with an argument which is the full filename of bat2.bat

With the code of bat1.bat it would take that first argument, echo it to the screen, then it would call that file as denoted by "call %1".

Here are the actual results:



--------------------------

"%userprofile%\Desktop\bat2.bat"



Hello World!

Press any key to continue. . .

--------------------------



In application, this batch file will delete the file that is dropped into it:



--------------------------

@echo off

del /f /q %1

exit

--------------------------



Fourth Part - The SHIFT Command



There is a way to move the positions of the arguments in a way with the SHIFT command. SHIFT moves each argument one index down from its original position.

So if you had these arguments:



--------------------------

%1 = Candle

%2 = Network

%3 = Beach

%4 = Book

--------------------------



and you would use the single word SHIFT as the command the outcome would move the argument values to this:



--------------------------

%0 = Candle

%1 = Network

%2 = Beach

%3 = Book

--------------------------



You could say that it will literally shift %4 to %3, %3 to %2, %2 to %1, and %1 to %0. The original %0 (filename) would be gone (unless you saved the value of %0 into another variable before the SHIFT command) and %4 would be empty.



Makes sense? Try it out.



Now if you don't want SHIFT to affect some of the arguments, you can specify the command to start shifting at a specified argument index. This specified index can be between 0 and 8.

For example, if you want your args to be shifted down to the 2nd argument but leave %1 and %0 unaffected then type:



--------------------------

SHIFT /2

--------------------------



Jokingly, I guess you could say that the number you specify for SHIFT is the argument that will have its value deleted.



So when applying it to:



--------------------------

%0 = {FileName}

%1 = Candle

%2 = Network

%3 = Beach

%4 = Book

--------------------------



The outcome of "SHIFT /2" would be:



--------------------------

%0 = {FileName}

%1 = Candle
rowlfe
2009-11-13 13:55:35 UTC
With a batch file, command line parameters are passed in through environment variables thus:



batch.bat param1 param2 param3 param4 etc...



Inside the batch file, %0 is the name of the batch file, %1 contains the contents of param1, %2 the contents of param2 etc.



-=+=-



@echo off

echo parameter 0 = %0

echo parameter 1 = %1

echo parameter 2 = %2

call program.exe %1 %2



-=+=-



In this 5 line file, parameters to the batch file are passed into program.exe through the environment.
James H
2009-11-13 13:27:26 UTC
It has been a long time since I have written a batch file. All I remember is basically to put in what you would type on a command line, i.e.: If you wanted to run windows you would type: c:\windows\win.exe Hope this helps
boduram
2016-10-19 08:50:27 UTC
this is a fashion which you're able to do it making use of Excel or another spreadsheet. Import the report into Excel. in case you have one get entry to in step with text fabric line, you will get one get entry to interior the 1st cellular of each and every row. variety the report so as that reproduction rows variety at the same time. In column B, row 2, define the cellular to be +IF(A2=A1), a million, 0) reproduction that formula down each and every of the rows of the spreadsheet. it is going to likely be 0 for the 1st merchandise of each and every reproduction set. shop the website as a text fabric report. The formula pass away and you shop in basic terms the text fabric and the 0's and a million's. Reopen the saved report in Excel and variety on column B. each and every of the 0 products would be at the same time. they are your unique products. Delete the different rows, sparkling column B, and shop.
ivory
2016-08-02 07:09:14 UTC
Improve Your Running Technique Now!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...