Send text to standard input of a Windows console app - batch-file

I am trying to write a .bat file to automate some shell commands. Most of the commands are easy and I can just put them into the batch file directly, but there is one command which instead of taking command line parameters, expects you to type in the options you want using "the standard input". I'm not exactly sure what that means. Can someone tell me how to do this? The text I would like to be entered is the contents of one of the files in the directory: "options.txt" which I want to concatenate with a variable inside the batch file "$(additionaloptions)".
Make sense?

The usual way to do this in .bat files is to use echo to write a small text file, then redirect the text file to standard in of the command.
#echo foo > bar.txt
#echo if you need multiple lines >> bar.txt
the_cmd < bar.txt
In your specific example it would something like
copy myfile.txt bar.txt
#echo %variable% >> bar.txt
The fact that you are mention $(variable) suggests to me that this is a makefile rather than a batch file. for a makefile, theres a better way.

Related

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)

Creating a dynamic .bat file

I was wondering if I could get some help with regards to writing a batch file that could take user input. (Or guidance if a batch file is not the way to go.)
I usually have a task that I do by opening CMD, navigating to a specific folder and running the following command
rda -v 848 -i "C:\me\rda-tools-1.7.0.Ra1\Input" -o "C:\me\rda-tools-1.7.0.RC1\Output"
Now this task is repetitive, and the only thing that changes each time is the number (848 in my example).
Can you guide me on how to write a batch file that navigates to the specific folder, asks me for that 3 digit number for input and then runs the command above?
Please note I have very little knowledge on batch files.
Thanks.
You can pass parameter to a batch file.
Follow below article on how to pass parameter to a batch file
How do I pass command line parameters to a batch file?
You can pass parameters to a batch file. They're taken in sequence, from %1 to %9 (you can use more, but you have to shift them to get them into position to use). (Technically, there is a %0 - it's the full path and filename of the batch file itself.)
For example, put the following into a batch file (for instance, RunRDA.bat):
#echo off
rda -v %1 -i "C:\me\rda-tools-1.7.0.Ra1\Input" -o "C:\me\rda-tools-1.7.0.RC1\Output"
Run it from a command prompt with your version:
C:\RDA>RunRDA 848
For more information, see How to pass command line parameters to a batch file?

Batch file: Reading and activate commands from unknown files

I know the title doesn't make sense, but I have one question. I have made this file called Test.bin and here's whats inside the .bin file:
echo Hello World
Since its a test file, I've been trying to see if i can make a batch file that can read the commands in the .bin file and output it onto the console.
Not sure what you are trying to do exactly, but I think you have two options:
Rename test.bin as test.bat and run it with:
test
Start a new command interpreter and send it your commands:
cmd < test.bin
You could also use the copy command. However, for this to work the test.bin file should be
in the same directory/folder. Alternatively, you can specify the file's path.
Your code should look something like this:
#echo off
copy test.bin
Or, using the filepath method (pretending its on your desktop):
#echo off
copy C:/users/me/Desktop/test.bin

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.

Read command-line parameters to .bat from file

I have a build.bat file which uses %1 internally... so you might call:
build 1.23
I wanted it to read the parameter from a separate file, so I tried putting "1.23" in version.txt and doing:
build < version.txt
But it doesn't work. Isn't this how piping works? Is what I want possible and if so how?
The FOR command in DOS has a form which parses files and assigns the tokens it finds to variables, which can then be used as arguments to other batch files. Assuming version.txt contains the single line "1.23", this batch file will open it, assign 1.23 to the variable, then call your original batch file, passing the variable's value as a command-line argument.
#echo off
for /f %%i in (version.txt) do call build.bat %%i
If version.txt contains more than one line, build.bat is called once for each line. Type help for at a DOS prompt to see what other features you might be able to use.
I think it would make more sense if you handle the file processing within the batch file on the off chance that version.txt is not edited correctly.
You should modify your script to parse the file to get the version if the .bat file is executed as:
build FILE version.txt

Resources