Creating a dynamic .bat file - batch-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?

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
)

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

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.

Windows batch file for iterative invocation of other batch files

Consider a directory structure containing the following files:
\1.3\Baseline\GeneratedScripts\One\FullInstall.cmd
\1.3\Baseline\GeneratedScripts\Two\FullInstall.cmd
\1.3\Baseline\GeneratedScripts\Three\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\One\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\Three\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\One\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\Three\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\One\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\Three\FullInstall.cmd
I would like to construct a Windows batch file InstallEnvironment.cmd which:
Takes an environment name as a parameter; then
Executes the baseline install script, and each of the patch scripts in turn.
The batch file should automatically execute any additional patches that are added later.
Essentially I need to do something along the lines of this:
for %%_ in (1.3\**\GeneratedScripts\%%1\FullInstall.cmd) do cal %%_
However I'm not sure the wildcard system is rich enough to allow this as I don't get any matches for the ** directory wildcard.
For example, calling with the parameter "Two" should execute the following scripts, in order:
\1.3\Baseline\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\Two\FullInstall.cmd
This will execute all the *.cmd files in the sub folders based on the argument:
for /r 1.3\ %%X in (GeneratedScripts\%1\*.cmd) do call "%%X"
In my experience, the %1 substitution works within directory names.
This should work:
InstallEnvironment.bat:
\1.3\Baseline\GeneratedScripts\%1\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\%1\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\%1\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\%1\FullInstall.cmd
Edit this batch file to add additional patches in order, and it works. If you need to run the same batch file on multiple directories, create another batch file:
call InstallEnvironment.bat %1
call InstallEnvironment.bat %2
If you want to run a batch file in the background, use a vbs file to run that bat file in background instead.
Here is the code:
CreateObject("Wscript.Shell").Run"""" & Wscript.Arguments(0)& """",0,False
Save this exactly as invisible.vbs (or anything) and then make another batch file which will call your batch file to run it in background.
The code for the second batch file is:
wscript.exe "invisible.vbs" "Your_Batch_File.bat"
Then run the second batch file.
Note: WSH should be enabled on your computer, and the invisible.vbs file and the second batch file should be in the same folder. If not then you can give the full path to the invisible.vbs file in the 2nd batch file's script.

Resources