Pass some positional parameters to subcommand in CMD - cmd

I have a CMD script that receives some parameters. It is called like this:
C:\myscript -A value -B value -C value -D -E -F value path1 path2 path3
It may receive several option-type parameters (not always the same number, may be none; some with argument, some without) and several path-type parameters (not always the same number, but always at least one). It is not an option to change that, because it is called by a program I can not control.
myscript must extract the path-type parameters only and pass them to one of its subcommands, like this:
#echo off
rem this is C:\myscript
C:\otherscript path1 path2 path3
I can not use numbered positional parameters like %1 %2 or %3 because I do not know in advance how many option-type parameters will there be, nor how many path-type options will there be.

With the additional benefit that the paths can show up anywhere in the command line, not just at the end. Assumption is per your example, each option and its immediately following parameter are being ignored, and anything that shows up without an option preceding is a path you want to use in the call.
#echo off
rem this is C:\myscript too :)
rem do this so it's repeatable, or use setlocal/endlocal
set parmlist=
:loop
set parm=%1
if '%parm:~0,1%'=='-' goto option
set parmlist=%parmlist% %parm%
shift
if '%1'=='' goto done
goto loop
:option
shift
if '%1'=='' goto done
set parm=%1
if not '%parm:~0,1%'=='-' shift
if not '%1'=='' goto loop
:done
c:\otherscript %parmlist%
Tested with this command line:
myscript -a fred -b wilma firstpath -c -d barney secondpath thirdpath
result:
c:\otherscript firstpath secondpath thirdpath
(yeah there's an extra space there but it's easier to read not to mash otherscript and %parmlist% right together)

After grokking a while with this issue, and thanks to some insight from Torqane's answer, I've developed a script that works:
SET SAVEPATH=
:loop
SET var1=%1
SET VAR1NOQUOTE=%~1
SET VAR1FIRST=%VAR1NOQUOTE:~0,1%
if "%VAR1NOQUOTE%"=="" goto continue
rem test known no-argument options
if "%VAR1NOQUOTE%"="-A" goto shift1
if "%VAR1NOQUOTE%"="-B" goto shift1
rem etc... as many as needed
rem test options with arguments
if "%VAR1FIRST%"="-" goto shift2
rem if not an aption, it is a path
SET SAVEPATH=%SAVEPATH% %VAR1%
shift
goto loop
:shift1
shift
goto loop
:shift2
shift
shift
goto loop
:continue
C:\otherscript %SAVEPATH%

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.

Using parameters in batch files at Windows command line

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

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

cmd: variable of open with

When, for example, i want a batch file to 'open' a file. when i for example drag and drop the file into the batch file, it should do some stuff with that file.
Now, i need to know the variable. I know there is a variable for this kind of stuff; i just forgot it.
Can someone give me the variable please?
Thanks.
The first 9 parameters given to a batch file can be accessed by writing %1 through %9.
The complete command line argument is stored in %*.
For more information, see here.
Drag&Drop to a batch file can be a much more difficult job.
Because windows doesn't know how to add the files in the correct way.
If your files are simple, it works as expected.
1.txt
2 3.txt
4 & 5.txt
drag.bat 1.txt "2 3.txt" "4 & 5.txt"
But some filenames are confusing windows...
6,7.txt
8&9.txt
drag.bat 6,7.txt 8&9.txt
-- results in --
%1 = 6
%2 = 7.txt
%3 = 8
%4 =
The command "9.txt" can not be found
In the first moment it seems an impossible problem,
but it exists a solution.
The trick is to use the cmdcmdline variable instead of the parameters %1..%9
The cmdcmdline contains something like
cmd /c ""C:\dragTest\test.bat" C:\dragTest\1.txt "C:\dragTest\2 3.txt"
C:\dragTest\6,7.txt C:\dragTest\8&9.txt"
So you can work with this, but you have to stop your batch after all, so the 9.txt can't be executed.
#echo off
setlocal ENABLEDELAYEDEXPANSION
rem Take the cmd-line, remove all until the first parameter
set "params=!cmdcmdline:~0,-1!"
set "params=!params:*" =!"
set count=0
rem Split the parameters on spaces but respect the quotes
for %%G IN (!params!) do (
set /a count+=1
set "item_!count!=%%~G"
rem echo !count! %%~G
rem Or you can access the parameter with, but this isn't secure with special characters like ampersand
rem call echo %%item_!count!%%
)
rem list the parameters
for /L %%n in (1,1,!count!) DO (
echo %%n #!item_%%n!#
)
pause
REM *** EXIT *** is neccessary to prevent execution of "appended" commands
exit

batch parameters: everything after %1

Duplicate:
Is there a way to indicate the last n parameters in a batch file?
how to get batch file parameters from Nth position on?
Clarification: I knew of the looping approach - this worked even before Command Extensions; I was hoping for something fun and undocumented like %~*1 or whatever - just like those documented at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true.
In a Windows batch file (with the so called "Command Extensions" on), %1 is the first argument, %2 is the second, etc. %* is all arguments concatenated.
My question: is there a way to get everything AFTER %2, for example?
I couldn't find such a thing, and it would be helpful for something I'm working on.
There is a shorter solution (one-liner) utilizing the tokenization capabilities of for loops:
:: all_but_first.bat
echo all: %*
for /f "tokens=1,* delims= " %%a in ("%*") do set ALL_BUT_FIRST=%%b
echo all but first: %ALL_BUT_FIRST%
output:
> all_but_first.bat foo bar baz
all: foo bar baz
all but first: bar baz
Footnote: Yes, this solution has issues. Same as pretty much anything written with batch files. It's 2021. Use Powershell or literally any other actual scripting language.
I am not sure if there is a direct command but you can always use a simple loop and shift to get the result in a variable. Something like:
#echo off
set RESTVAR=
shift
:loop1
if "%1"=="" goto after_loop
set RESTVAR=%RESTVAR% %1
shift
goto loop1
:after_loop
echo %RESTVAR%
Let me know if it helps!
The following will work for args with ", =, ' '. Based on Dmitry Sokolov answer. Fixed issue when second arg is the same as first arg.
#echo off
echo %*
set _tail=%*
call set _tail=%%_tail:*%1=%%
echo %_tail%
The following will work for args with ", =, ' ' (as compared to #MaxTruxa answer)
echo %*
set _all=%*
call set _tail=%%_all:*%2=%%
set _tail=%2%_tail%
echo %_tail%
Test
> get_tail.cmd "first 1" --flag="other options" --verbose
"first 1" --flag="other options" --verbose
--flag="other options" --verbose
You can use SHIFT for this. It removes %1 and shifts all other arguments one lower. This script outputs all the arguments after %2 (so it outputs %3, %4...) until one of them is empty (so it's the last one):
#echo off
SHIFT
SHIFT
:loop
if "%1" == "" goto end
echo %1
SHIFT
goto loop
:end
EDIT: Removed example using %* as this doesn't work - %* always outputs all of the parameters
Building on schnaader's answer, I think this does it if you want everything after %1 concatenated.
#echo off
SHIFT
set after1=
:loop
if "%1" == "" goto end
set after1=%after1% %1
SHIFT
goto loop
:end
echo %after1%
Sebi, here's the Syntax!
There is a behavior, batch eating the equal signs which is not double quoted, it cause trouble in the scripts above. If you wan't to skip, i've made a modification, based on Raman Zhylich answer and strlen.cmd:
#ECHO OFF
SETLOCAL enableDelayedExpansion
SET _tail=%*
SET "_input="
SET /A _len=0
:again
SET "_param=%1"
SET "_input=%_input%%1"
FOR /L %%i in (0,1,8191) DO IF "!_param:~%%i,1!"=="" (
REM skip param
SET /A _len+=%%i
REM _len can't be use in substring
FOR /L %%j in (!_len!,1,!_len!) DO (
REM skip param separator
SET /A _len+=1
IF "!_tail:~%%j,1!"=="=" (SET "_input=%_input%=" & SHIFT & goto :again)
)
) & goto :next
:next
IF %_len% NEQ 0 SET _tail=!_tail:~%_len%!
ENDLOCAL & SET "_input=%_input%" & SET "_tail=%_tail%"

Resources