John
John

Reputation: 86

Use parameter as intermediate input in Batch File

In a batch file, I am running exe which takes input from user. I want to hardcode value and continue process. check following example:

In Bat File(GetData.bat):

set /p UserInput = Enter a number?
%1

To call bat file:

GetData 5

but it's waiting for input instead of setting 5.

Note: It's just for example actually i am calling exe which takes input in process. I can't modify exe.

Upvotes: 1

Views: 4605

Answers (2)

dbenham
dbenham

Reputation: 130819

Command line parameters are not the same thing as standard input (stdin). There are some programs that can accept input both as command arugments or via stdin, but that is not the norm.

To get your batch script to work, you must ECHO the data to stdout, and then pipe the results to GetData.bat

echo 5|GetData

If you must provide multiple lines of input, then you can do something like

( 
  echo line1
  echo line2
  echo line3 etc.
) | yourProgram.exe

Or you can put the input in a text file and then pipe the contents of the file to your command.

type commands.txt | yourProgram.exe

Or you can redirect the input to your file

<commands.txt yourProgram

Note that some programs read the keyboard directly, and or flush the input buffer before prompting for input. You will not be able to pipe input into such a program.

Update - exe not using piped input

Most database command line programs that I have seen have an option to specify the username and password as command line arguments. You should read the documentation, or google your exe for command line arguments.

If your exe does not have an option to specify the values on the command line, then your only option is to use a 3rd party tool like AutoHotKey that can be scripted to pass keystrokes to a running program.

Upvotes: 2

jaselg
jaselg

Reputation: 377

Look at this example:

echo Enter a number?
set UserInput=%1
echo %UserInput%

Then if you type "GetData 5", the output will look like this:

Enter a number?
--empty line to execute "set"
5

Upvotes: 0

Related Questions