How to control .exe file with .bat file? - batch-file

Hi I have program Program.exe which have some 3 parameters. And I am trying to control this program via .bat file. I can run this program with parameters without any problems, its easy. But if this program starts succesfully, then there are two choices what to do. For example if user type 'a' then this program writes on console 'hi'. If user types 'b' then program writes to console 'hello'. I want to somehow sent to this Program.exe only the 'a' choice after start. Unfortunaly I cant add another parameter. Does anyone know how to do it?
So here is my very simple .bat file.
Program %1 %2 %3
and I tried to add this command to .bat file but it doesnt works.
echo a > Program
Thanks for help.

Try:
Echo a| Program %1 %2 %3
That is a known method for most CLI apps, but is not valid for all applications, if the pipe don't work for your app then forget to do it using native Batch and try a nircmd sendkey command or another CLI app to send keys.
Pipes: http://ss64.com/nt/syntax-redirection.html
NirCMD: http://www.nirsoft.net/utils/nircmd.html

Related

How to disable input in batch script?

I have a batch script (mine) that launches another batch script (theirs). Their batch script queries user input as part of the process, and I do not have access to modify it.
I need to suppress their batch script from querying input, meaning that when their batch script outputs: Press enter for step 2..., I want the user to be unable to interact with the script using their keyboard, hence the script should look like it's frozen.
How do I call their script from my script in such a way that the user is unable to interact with the input requests of their script?
Actually summing up the comments to make this not an unanswered question. You can try using the | (pipe) operator:
echo Haha! You will not be able to press any key!! | their.bat
Which will redirect STDOUT (Standard Out) of command echo Haha! You will not be able to press any key!! (Haha! You will not be able to press any key!!) to STDIN (Standard Input) of command theirs.bat.
Or, even read from nul:
their.bat < nul
I recommend reading https://ss64.com/nt/syntax-redirection.html and What does "&&" in this batch file? (second answer is better here.).

How to make batch file to run exe file which requires some text

I need to run 'a.exe'.
When I start 'a.exe' file, a console pops up, and I should type "go", then the program start.
If i want to make a batch file to run this program, how should i make this.
I tried as below:
///////////
%~d0
cd %~dp0
start a.exe > "go"
pause
///////////
but "go" appears on the batch console, and the "a.exe" program still requires "go" text.
How can i solve this?
Assuming you are coding a windows .bat script, you can execute any file by just passing the file with its path and its argument on the same line a.exe go, so your script would be something like this:
#echo off
c:\path\to\file\a.exe go
PAUSE
I am turning off CMD echo so the output of a.exe is easier to be read.
There is another question here were this is further explained: Create a batch file to run an .exe with an additional parameter.
On the other hand, if you need to call another script, you would need to use the method call
#echo off
call c:\path\to\other\script\script.bat
c:\path\to\file\a.exe go
PAUSE
You can use:
start echo go| a.exe
Just type in
a.exe go
Hope it helped you out.
You can also use it to run files through certain programs
photoshop img.png

How to pass commands in a text file to an EXE running through the command line with batch?

I have a text file with lots of commands in it and I want to sent those commands to a software called thermocal. It is a console application. I found the command below, but it doesn't work for me.
Do I need to put this .exe file in the same folder of the batch file to make it work or any thing else?
type somefile.txt | Thermocal.exe
Batch scripts can be considered as a collection of lines you could also type in a command line prompt one after another. With respect to this it might be helpful for you, to play with cmd in order to get a feeling for what is happening.
About starting thermocal: Assuming thermocal is not part of PATH then the batch file either needs to change the current directory to the one with termocal.exe. Alternatively you might be able to call thermocal.exe with adding a path like C:\ProgramFiles\Thermocal\thermocal.exe . Play with cmd to find out, what works and what doesn't
When you are able to start thermocal from the command line prompt window, you can start experimenting with the call. You will probably end of with something like this in your command line window:
C:\ProgramFiles\Thermocal> thermocal argument1 argument 2
If this works, you can start with batch programming :)
Assuming your arguments are stored in somefile.txt like this:
argument1 argument 2
TYPE does nothing more than printing a file:
TYPE somefile.txt
Now you need to use the result of the output as command line arguments:
for /f %%i in ('type somefile.txt') do (thermocal.exe %%i)

how to have a batch file answer questions automatically for an exe file?

Alright so excuse my lack of knowledge, just actually started learning about coding and am trying something out with a few programs.
I have an .exe and a .bat file that I want to string together so that I have one .bat file that I run. I would rather not edit the other bat files. The problem is the .exe file l poses two questions and creates a file.mesh. Now the first question's answer is always 0 so I'm wondering if there's a way to enter the 0 outside of the exe file, and the second is the name of a file which I want to keep as user input.
In addition to that, I want to take the name that I just put in in the second question to automatically run through a second batch file which creates another file...
So essentially what I want is to have the .bat open and ask for a name of a file, then run both the exe and the other .bat. can anyone give me some direction as to where I should start researching or could help me write?
Depending on how the EXE file is written, this may or may not work. Try it and see.
>file.txt echo 0
>>file.txt echo filename.bin
exefile.exe <file.txt
or
>file.txt echo 0
>>file.txt echo filename.bin
type file.txt|exefile.exe
Simply a line in HelloWorld.bat should read:
Echo 0 ¦ foobar.exe

batch language tutorials - running simple programs that rely on .bat file

I am just starting to get a handle on batch programming.
To date, I've been copy/pasting into the MS-DOS command prompt from a text editor. Some of these copy pastes are getting large. Im sure there is a better way to go about this, ie. writing a line in command prompt that calls other text files (effectively doing the work of copy pasting).
Are these external files going to be .bat (which are just text that could also be put directly into the command prompt?) or .txt or something else?
I am mainly looking into this so that I can get into reusing code and getting into looping.
Are there any tutorials someone would recommend to get my acquainted with these topics?
Thanks for any help.
You can name a text file .bat or .cmd (the latter if you know it's only usable as a Windows batch file) and put commands into it, line by line.
You can run such files by typing their name at the command prompt when you're in the directory where they reside (or if they are contained in one of the PATH directories).
By default the behavior will match pretty much exactly with what you would type by hand. You'll see what commands are executed as well as their output. For example the following batch file (saved as test.cmd here)
echo Hello World
dir /b *.cmd
yields the following output when run
> echo Hello World
Hello World
> dir /b *.cmd
date.cmd
foo.cmd
test.cmd
x.cmd
y.cmd
You can suppress the output of the command being run by including the line
echo off
in your batch file. Prefix it with an # to suppress command output for that line in particular, but ever subsequent command won't be echoed:
#echo off
If other concrete questions arise, feel free to ask.

Resources