1

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It's not working for the exe, It seems need of keyboard input. Basically I want to connect ODBC without asking password to run in background, so I'm passing with bat file argument.
@John - Well then, piped input won't work. You have 2 more options. See updated answer.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.