Using parameters in batch files at Windows command line - windows

In Windows, how do you access arguments passed when a batch file is run?
For example, let's say I have a program named hello.bat. When I enter hello -a at a Windows command line, how do I let my program know that -a was passed in as an argument?

As others have already said, parameters passed through the command line can be accessed in batch files with the notation %1 to %9. There are also two other tokens that you can use:
%0 is the executable (batch file) name as specified in the command line.
%* is all parameters specified in the command line -- this is very useful if you want to forward the parameters to another program.
There are also lots of important techniques to be aware of in addition to simply how to access the parameters.
Checking if a parameter was passed
This is done with constructs like IF "%~1"=="", which is true if and only if no arguments were passed at all. Note the tilde character which causes any surrounding quotes to be removed from the value of %1; without a tilde you will get unexpected results if that value includes double quotes, including the possibility of syntax errors.
Handling more than 9 arguments (or just making life easier)
If you need to access more than 9 arguments you have to use the command SHIFT. This command shifts the values of all arguments one place, so that %0 takes the value of %1, %1 takes the value of %2, etc. %9 takes the value of the tenth argument (if one is present), which was not available through any variable before calling SHIFT (enter command SHIFT /? for more options).
SHIFT is also useful when you want to easily process parameters without requiring that they are presented in a specific order. For example, a script may recognize the flags -a and -b in any order. A good way to parse the command line in such cases is
:parse
IF "%~1"=="" GOTO endparse
IF "%~1"=="-a" REM do something
IF "%~1"=="-b" REM do something else
SHIFT
GOTO parse
:endparse
REM ready for action!
This scheme allows you to parse pretty complex command lines without going insane.
Substitution of batch parameters
For parameters that represent file names the shell provides lots of functionality related to working with files that is not accessible in any other way. This functionality is accessed with constructs that begin with %~.
For example, to get the size of the file passed in as an argument use
ECHO %~z1
To get the path of the directory where the batch file was launched from (very useful!) you can use
ECHO %~dp0
You can view the full range of these capabilities by typing CALL /? in the command prompt.

Using parameters in batch files: %0 and %9
Batch files can refer to the words passed in as parameters with the tokens: %0 to %9.
%0 is the program name as it was called.
%1 is the first command line parameter
%2 is the second command line parameter
and so on till %9.
parameters passed in on the commandline must be alphanumeric characters and delimited by spaces. Since %0 is the program name as it was called, in DOS %0 will be empty for AUTOEXEC.BAT if started at boot time.
Example:
Put the following command in a batch file called mybatch.bat:
#echo off
#echo hello %1 %2
pause
Invoking the batch file like this: mybatch john billy would output:
hello john billy
Get more than 9 parameters for a batch file, use: %*
The Percent Star token %* means "the rest of the parameters". You can use a for loop to grab them, as defined here:
http://www.robvanderwoude.com/parameters.php
Notes about delimiters for batch parameters
Some characters in the command line parameters are ignored by batch files, depending on the DOS version, whether they are "escaped" or not, and often depending on their location in the command line:
commas (",") are replaced by spaces, unless they are part of a string in
double quotes
semicolons (";") are replaced by spaces, unless they are part of a string in
double quotes
"=" characters are sometimes replaced by spaces, not if they are part of a
string in double quotes
the first forward slash ("/") is replaced by a space only if it immediately
follows the command, without a leading space
multiple spaces are replaced by a single space, unless they are part of a
string in double quotes
tabs are replaced by a single space
leading spaces before the first command line argument are ignored

#Jon's :parse/:endparse scheme is a great start, and he has my gratitude for the initial pass, but if you think that the Windows torturous batch system would let you off that easy… well, my friend, you are in for a shock. I have spent the whole day with this devilry, and after much painful research and experimentation I finally managed something viable for a real-life utility.
Let us say that we want to implement a utility foobar. It requires an initial command. It has an optional parameter --foo which takes an optional value (which cannot be another parameter, of course); if the value is missing it defaults to default. It also has an optional parameter --bar which takes a required value. Lastly it can take a flag --baz with no value allowed. Oh, and these parameters can come in any order.
In other words, it looks like this:
foobar <command> [--foo [<fooval>]] [--bar <barval>] [--baz]
Complicated? No, that seems pretty typical of real life utilities. (git anyone?)
Without further ado, here is a solution:
#ECHO OFF
SETLOCAL
REM FooBar parameter demo
REM By Garret Wilson
SET CMD=%~1
IF "%CMD%" == "" (
GOTO usage
)
SET FOO=
SET DEFAULT_FOO=default
SET BAR=
SET BAZ=
SHIFT
:args
SET PARAM=%~1
SET ARG=%~2
IF "%PARAM%" == "--foo" (
SHIFT
IF NOT "%ARG%" == "" (
IF NOT "%ARG:~0,2%" == "--" (
SET FOO=%ARG%
SHIFT
) ELSE (
SET FOO=%DEFAULT_FOO%
)
) ELSE (
SET FOO=%DEFAULT_FOO%
)
) ELSE IF "%PARAM%" == "--bar" (
SHIFT
IF NOT "%ARG%" == "" (
SET BAR=%ARG%
SHIFT
) ELSE (
ECHO Missing bar value. 1>&2
ECHO:
GOTO usage
)
) ELSE IF "%PARAM%" == "--baz" (
SHIFT
SET BAZ=true
) ELSE IF "%PARAM%" == "" (
GOTO endargs
) ELSE (
ECHO Unrecognized option %1. 1>&2
ECHO:
GOTO usage
)
GOTO args
:endargs
ECHO Command: %CMD%
IF NOT "%FOO%" == "" (
ECHO Foo: %FOO%
)
IF NOT "%BAR%" == "" (
ECHO Bar: %BAR%
)
IF "%BAZ%" == "true" (
ECHO Baz
)
REM TODO do something with FOO, BAR, and/or BAZ
GOTO :eof
:usage
ECHO FooBar
ECHO Usage: foobar ^<command^> [--foo [^<fooval^>]] [--bar ^<barval^>] [--baz]
EXIT /B 1
Yes, it really is that bad. See my similar post at https://stackoverflow.com/a/50653047/421049, where I provide more analysis of what is going on in the logic, and why I used certain constructs.
Hideous. Most of that I had to learn today. And it hurt.

Batch Files automatically pass the text after the program so long as their are variables to assign them to. They are passed in order they are sent; e.g. %1 will be the first string sent after the program is called, etc.
If you have Hello.bat and the contents are:
#echo off
echo.Hello, %1 thanks for running this batch file (%2)
pause
and you invoke the batch in command via
hello.bat APerson241 %date%
you should receive this message back:
Hello, APerson241 thanks for running this batch file (01/11/2013)

Use variables i.e. the .BAT variables and called %0 to %9

Related

How to obtain optional command line argument value as a string in windows batch script?

Given an optional port argument, where the port number can vary in length, how do I obtain the port number from the batch script's command line arguments?
Example:
foo.bat --foo bar --port 80 --bar foo
Should output:
80
I got this far, trying to use substring.
set CMD_LINE_ARGS=%*
#rem Remove all chars and port arg
set PORT_ARG_REMOVED=%CMD_LINE_ARGS:*-port =%
#rem Obviously, this is where I fail to remove trailing chars
set PORT_NUM=%PORT_ARG_REMOVED: *=%
echo %PORT_NUM%
Edit
The answer I chose is because it fits with my very particular use case, where all arguments were being passed through to the command that I was wrapping. And, I only needed the value for a particular optional argument. No looping required.
There are some very nice answers here for dealing with optional argument parsing in general. So, feel free to upvote everyone.
Seeing as you 'would have really have liked to see the substring work', here's your script structured and coded as you'd intended.
#Set "CMD_LINE_ARGS=%*"
#Rem Remove all chars and port arg
#Set "PORT_ARG_REMOVED=%CMD_LINE_ARGS:*-port =%"
#Rem Remove trailing chars
#Set "PORT_NUM=%PORT_ARG_REMOVED: ="&:"%"
#Echo %PORT_NUM%
#Pause
You can use %1, %2 etc for the separate command line arguments. In your case --port would be %3 and its value would be %4.
Fortunately, there is also the shift command, which shifts all arguments, to 2 becomes 1, 3 becomes 2, etc.
That means that you can 'scrape' all command line parameters in a loop. You keep shifting and when you encounter --port, you know that the next one is going to be the port number, which you can store in an appropriate variable (using set) for later use.
If you implement it like that, you can implement a bunch of optional parameters and the order won't matter either.
So in code, your 'foo.bat' could look like this:
#echo off
:: No parameters given? Then run ourselves with the defaults from the question.
if "%1"=="" goto none
:: Actual reading of parameters starts here.
:loop
if "%1"=="--port" set port=%2
:: Of course you need the other ifs only if you're interested in these parameters
if "%1"=="--foo" set foo=%2
if "%1"=="--bar" set bar=%2
:: Shift all parameters, except %0. Do it twice to skip the parameter and its value.
shift /1
shift /1
:: As long as there are more, keep looping. You can even parse more than 10 parameters this way!
if not "%1"=="" goto loop
:: Output what we've found
echo The specified port is %port%
echo Foo is set to %foo%
echo The bar is at %bar%
pause
exit /b
:: If no parameters are given, call ourselves with example parameters.
:none
call %0 --foo bar --port 80 --bar foo
Dressed down version without the demo crap that only displays the port number. I think this is a drop-in replacement for your current code.
#echo off
:loop
if "%1"=="--port" set port=%2
shift /1
shift /1
if not "%1"=="" goto loop
echo %port%
A simpler and obvious approach that get all arguments no matters how many:
#echo off
setlocal EnableDelayedExpansion
set "arg="
for %%a in (%*) do (
if not defined arg (
set "arg=%%a"
) else (
set "!arg:~2!=%%a"
set "arg="
)
)
echo foo = %foo%
echo port = %port%
echo bar = %bar%
If you like short code, the version below do the same task in less lines:
#echo off
set "args= %*"
set "args=%args: --=+%"
set "args=%args: ==%"
set args=%args:+=&set %
echo foo = %foo%
echo port = %port%
echo bar = %bar%
If you want to review how the shorter version works, just remove the #echo off line and execute it.
I would first remove the --port part using sub-string replacement, then get the value by a for /F loop, like this:
set CMD_LINE_ARGS=%*
#rem Remove everything up to `--port` + a space:
set "PORT_ARG_REMOVED=%CMD_LINE_ARGS:*--port =%"
#rem Extract first space- (or tab-)separated item:
for /F "tokens=1" %%A in ("%PORT_ARG_REMOVED%") do set "PORT_NUM=%%A"
echo/%PORT_NUM%
The for /F approach has got the great advantage that it even works when multiple consecutive SPACEs are provided to separate the command line arguments.
If you want the code to allow all standard token separators (which are SPACE, TAB, ,, ;, = and the character with code 0xFF) rather than only the SPACE, you can change the code to this:
set "FLAG="
#rem Use a standard `for` loop to walk through the arguments:
for %%A in (%*) do (
#rem Capture the argument following the `--port` item:
if defined FLAG if not defined PORT_NUM set "PORT_NUM=%%~A"
#rem Use a `FLAG` variable to delay capturing argument by an iteration;
#rem to match `--port` case-sensitively, remove the `/I` option:
if /I "%%~A"=="--port" set "FLAG=#"
)
echo/%PORT_NUM%
Actually I prefer that way, because there is no string manipulation involved, because the list of arguments is parsed in the nature of cmd.

Batch file: parsing command line inputs

I am working on a batch file to use as a wrapper for some data processing modules that are already written, with the goal of being able to run some in parallel and others serially as needed/processing power allows. The underlying modules require one input (a path contained in double quotes), and optionally another path contained in double quotes and/or a set of flags, and I am trying to figure out how to test these inputs. I currently have
:TEST_PARAMS
REM Make sure parameters are correct; if not, display a usage message
IF NOT "%1"=="" (SET SUBBASENAME=%1
) ELSE (
GOTO :USAGE
)
IF NOT "%2"=="" (SET ATLASBASENAME=%2
) ELSE (
SET ATLASBASENAME=%DEFAULTATLAS%
)
IF NOT "%3"=="" (SET FLAGS=%3
) ELSE (
SET FLAGS=""
)
GOTO :START_SVREG
This seems to parse everything correctly if things are input in the correct order. However, I also wanted to check if the flags (which will be preceeded by '-') are popping up as the first or second inputs, and if they are, to display a usage message/set the variables correctly as needed. I figured the easiest way to do this would be to see if the first character of these strings is a '-', but I can't find any way to do this. I found a snippet of code that checks if a string contains a certain substring by replacing the substring with empty strings and then seeing if the resulting string is the same as the original, but this wouldn't work as people might legitimately have hyphens somewhere else in their path(s). Is there a way to check whether the first character of a string matches a given character in a batch file, or a better way of doing what I want?
You really don't need to check "-" in any batch file argument because as a batch file creator you will know what commands can be processed or not. Means if use "-b" then you know how to process it or if user passed "-t" you will know how to process. In your batch file you just need to process all the arguments, no matter in which order they are, and in your batch file you will have sections to process it. Here is an example Batch file which can process any number of arguments with "-" or "/" used with them:
#echo off
if [%1]==[] goto usage
:CHECKFORSWITCHES
#echo Execute the Command
IF '%1'=='/param1' GOTO PARAM1
IF '%1'=='-param2' GOTO PARAM2
IF '%1'=='/param3' GOTO PARAM3
IF '%1'=='-param4' GOTO PARAM4
IF '%1'=='/param5' GOTO PARAM5
goto :eof
:PARAM1
#echo This is Param1
set var=%1
set var2=%var:~0,1%
echo %var2%
SHIFT
goto :CHECKFORSWITCHES
:PARAM2
#echo This is Param2
SHIFT
goto :CHECKFORSWITCHES
:PARAM3
#echo This is Param3
SHIFT
goto :CHECKFORSWITCHES
:PARAM4
#echo This is Param4
set var=%1
set var2=%var:~0,1%
echo %var2%
SHIFT
goto :CHECKFORSWITCHES
:PARAM5
#echo This is Param5
SHIFT
goto :CHECKFORSWITCHES
:usage
#echo Usage: %0 ^<Write your command here^>
exit /B 1
Above script also checks for very first character of any parameter so if you need to use that code separately, you sure can use it however I don't think it is a good way to do it.
Above if you need to write some logic based in "-" or "/", just use IF %var2%=='/" GOTO XXX and you can do whatever you are looking for.
You can transfer the argument to an environment variable, and then use a SET substring operation to look at the first character. (Type HELP SET from the command line to get info about substring operations)
#echo off
set "var=%~1"
if defined var if "%var:~0,1%" equ "-" echo arg 1 is a flag

How to pass multiple params in batch?

In my batch file I want to pass multiple parameters to some other application.
Now I do it
app.exe %1 %2
and it can only pass two parameters, but I want to pass all the parameters that are passed to the batch(I would rather not write %1 %2 %3 %4 ...)
Is there any magic way to do it?
There is a magic way! I knew it, but I could not remember it.
its %*
You could use the SHIFT prompt and loop through the arguments. Here is a demonstrative example where you would replace the final ECHO prompt with a prompt to load your application.
#ECHO OFF
SET PARAMS=
:_PARAMS_LOOP
REM There is a trailing space in the next line; it is there for formatting.
SET PARAMS=%PARAMS%%1
ECHO %1
SHIFT
IF NOT "%1"=="" GOTO _PARAMS_LOOP
ECHO %PARAMS%
PAUSE
This may be useful if you need some sort of dynamic parameter counting, or if you want to disallow a certain parameter.
Another way is to use one double quoted parameter. When calling the other application, you just use the %~N device on the command line to remove the quotes.
If some parameters you intend to pass to the application are themselves double-quoted, those quote chars must be repeated twice.
Here's an illustration script that uses the first parameter as the application's name and the second as a combined parameter list to pass to the application:
#ECHO OFF
CALL %1 %~2
Here are examples of calling the script for different cases (pass one parameter or several parameters or quoted parameters).
Pass 1 parameter to the app:
C:\>mybatch.bat app.exe "app_param"
C:\>mybatch.bat app.exe app_param
Pass several parameters:
C:\>mybatch.bat app.exe "app_param1 app_param2 app_param3"
Pass a parameter that includes spaces (and so must be quoted):
C:\>mybatch.bat app.exe """parameter with spaces"""
A combined example: some parameters are with spaces, others aren't:
C:\>mybatch.bat app.exe "param_with_no_spaces ""parameter with spaces"" another_spaceless_param"

Spaces in batch script arguments

I have a batch script which needs to perform an action on each of its arguments. Each argument is a file name (there are not switches), and of course file names may contain spaces. The batch script is run either by dragging files into the .bat icon in Explorer or by entering the files at the command line, enclosing arguments with spaces in quotes.
Within the batch script, there are problems with handling arguments with spaces. If I use %* as follows, the quotations are ignored and each 'word' between spaces is treated as an argument.
for %%x in (%*) do (
echo %%x
)
I have also tried using shift, which doesn't seem to work right either, choking on files with spaces in their name:
:next
if not %1 == "" (
echo %1
shift /1
goto next
)
What is the ideal way to iterate through all arguments?
In Bash, one would simply use "$#" and everything Just Works™, but of course that doesn't seem to be the case with Windows batch scripts.
The substitution modifiers for for variable references also allow for using the ~ expansions. See for command reference.
By using "%%~x" you should get a properly quoted parameter, similar to how bash handles "$#".
#echo off
setlocal enableextensions
for %%x in (%*) do (
echo "%%~x"
)
The characters , and ; can be used to separate command parameters. See command shell overview. Thus you have to put quotes around file names that contain these characters.
If you drag a file from the Explorer onto the .bat, Explorer will only quote the file correctly if it has a white space character in its path. E.g., D:\a,b,c.exe will not be quoted by Explorer and thus will be parsed as three separate arguments by cmd.exe.
To make the script work with drag and drop from the Explorer for these freak cases, you can use the following (ugly) work-around:
#echo off
setlocal enableextensions enabledelayedexpansion
set "args=%*"
set "args=%args:,=:comma:%"
set "args=%args:;=:semicolon:%"
for %%x in (%args%) do (
set "filepath=%%~x"
set "filepath=!filepath::comma:=,!"
set "filepath=!filepath::semicolon:=;!"
echo "!filepath!"
)
The script introduces a helper variable args, where each occurrence of a troublesome character is replaced with a placeholder (note that the colon character itself cannot be used in a file name under Windows).
The body of the for loop uses another helper variable filepath which undos the transformation to produce the original path.
I had a similar issue with file names that contain equal signs (=), which causes the file name to be split into multiple arguments. I solved it by using "%*".
If you have a file with spaces, e.g. foo bar baz.txt, this will be quoted twice: ""foo bar baz.txt"". Now the double double-quotes are escaped: foo bar baz.txt, resulting in %1 = foo, %2 = bar, and %3 = baz.txt. So this does not work.
If you have a file with spaces AND/OR equal signs, you can use:
set input=""%*""
set input=%input:"=%
your_program "%input%"
Now, foo bar=baz.txt will be quoted thrice: """foo bar=baz.txt""". Two quotes will be escaped and input becomes "foo bar=baz.txt". With the second line, double-quotes are replaced by nothing (removed). You need to put the quotes around input again when you enter it into your_program, otherwise it will see spaces as separate input!
If you only have equal signs, then ""%*"" makes foo=bar=baz.txt into ""foo=bar=baz.txt"", which enters your program as %1 = foo=bar=baz.txt.

how to use dos commands to do following

At the following location
\ncsusnasent02.na.jnj.com\its_diq_na_win_dev\PowerCenter\infa_shared\WCPIT_BIO_EDW\SrcFiles\DDDMD\DDD.CLI026.WK0933.DDDMR45.001.head
I have one file
DDD.CLI026.WK0933.DDDMR45.001.head
if i open this file
i get data as following(in a single line)
HEADER0101IMS HEALTHDMD Weekly D DD.CLI026.WK0933.DDDMR45 Centocor DMDDRM45 W2009080210120090831125325ssnyder#us.imshealth.com
TRAIL0101 000000000581 0000000000CKSUM000002236804730
we need to copy 581(it will not be same always it gets updated everyday) from this file
and put it in a variable
you can try the below. It will set the field into the environment variable id:
for /f "tokens=10" %%a IN (%1) do (
SET id=%%a
)
echo %id%
You can pass the full path and file name into the bat as the first argument.
edit:
This simple bat will take the input from the file you specify on the commandline (param %1), it will use the default separators of <space> and <tab> to break the line in your file - defined in the IN set - into a set of tokens. The "tokens=10" param tells the processor to pass the 10th token, which turns out to be your number in question, into the DO block. It is passed in as a param %%a. Within the DO block, I simply assign that value to an environment variable id. After the for command is complete, I echo the value out to the console.
Take a look at the FOR command, specifically the part about the /F parameter.
I'm not certain enough about the structure of that line to even try to write the full command, but you should be able to write it yourself given that information.
Hmm to me it looks more like the guy needs a dos substr... i.e.
#Echo Off
If not %1.==[]. (Cmd /V:On /C Call %0 [] %1 & GoTo :EOF)
Shift
Set MyVariable=HELLOWORLD
Set ASubStr=!MyVariable:~%1!
Echo [!ASubStr!]
So for example save this as test.bat and then call "test.bat 5" and it will echo WORLD
Google DOS Substring and work out how to parse your text variable the way you want it.

Resources