Batch file: Reading and activate commands from unknown files - batch-file

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

Related

How to create a batch script which takes list of files as argument and passes to an exe

I am trying to create a batch file which runs an exe with arguments.
The exe needs an input file as argument.
Instead of specifying file name as argument, I would like to consider all files residing in that folder as input to exe.
Below is the syntax I have used in the batch script and executed it. Upon running below code batch script stopping suddenly.
However, when I specify the file name and run from command prompt the execution is successful.
Example:
Example.exe -i c:\Test\*.wav -o c:\result
I have looked at few of the examples in Google and found that I can use *.fileextension to refer that particular files in the folder.
I am thinking if there is a better approach to achieve this.
I am assuming you will run the same command and switch for multiple wav files.
#echo off
for %%i in (*.wav) do (
example.exe -i %%i -o c:\result
)

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

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 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.

Send text to standard input of a Windows console app

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.

Resources