Split the passed parameter, then access all elements - batch-file

My .bat file is supposed to execute command passed as the first argument, %1, which takes a list of parameters that are contained within the second parameter passed, %2.
Note that the second file is interpreted as a sentence, but is in fact a list of words. Therefore, program %1 complains as it has access to only one element. How can I split the second argument, %2, and invoke the correct format %1 %firstArgOf1 %secondArgOf1 ...
I am interested in solutions where the number of words in %2 is know in advance, but a variant with an unknown number of words in %2 is welcome.
EXAMPLE:
Program "myProgram" accepts 6 parameters. Any other number of parameters passed results in an error:
myProgram "this is one sentence passed as a single parameter"
However, myProgram works correctly in the following:
myProgram this is one sentence passed as a single parameter
The thing is, parameter "this is one sentence passed as a single parameter" is passed a a string (and this is fixed), so I have to find a workaround in a .bat file to split this argument.

#ECHO OFF
SETLOCAL
SET "parm1=%~1"
SET "parmrest=%*"
CALL SET "parmrest=%%parmrest:*%~1=%%"
ECHO(command is "%parm1%" with arguments "%parmrest%"
GOTO :EOF
This should accomplish your partition.

To remove quotes from a parameter, you can use the syntax %~1.
In a batch file it looks like
#echo off
myProg.exe %~1

Related

Passing argument containing dot to a batch script

So I'm having trouble passing an argument with a period to a batch script file.
./myScript.bat 23.97
In my script, if I do
arg1 = %1
echo %arg1%
This will display 23.97 but if I do a comparison
arg1 = %1
if "%arg1%" == "23.97"
echo %arg1%
then it doesn't display at the argument at all. Fyi, i'm not trying to treat it at a float number, just a normal string. I'm no sure why it doesn't work, any help is appreciated. Thank you.
You can, in fact, have a dot (.) passed to a batch file as an argument. The section of your code that is causing you issue is the syntax that you use when setting the variable and using the if statement.
The correct syntax for setting a variable , as described by executing help set on the command line, is
Displays, sets, or removes cmd.exe environment variables.
SET [variable=[string]]
variable Specifies the environment-variable name.
string Specifies a series of characters to assign to the variable.
With this in mind, the correct way to set arg1 to the first argument passed to your batch file is
set arg1=%1
Your issue with the if statement is that you are trying to add a new line after the Boolean expression and before the next statement. The correct syntax is described by help if as
Performs conditional processing in batch programs.
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
Your if statement could be reveised to read like this:
if "%arg1%"=="29.37" echo arg1

Accessing unknown number of commands (parameters) in batch file

this one's a bit difficult to explain, but I'll do my best.
I'm passing a list of directories into a batch file via a string array, which is created in Java and then passed into the .bat using Runtime.getRuntime().exec(commands). The trouble I am having is in regards to accessing the commands array, the size of which may vary from execution to execution. For example, during one run, "Commands" may contain the following:
{"cmd.exe", "/C", "Start", "program.bat", "stringA", "stringB", "stringC"}
The first four elements are used to call the batch file, so only strings A, B, and C are passed into the batch file (program.bat) as parameters. However, on the next run, "commands" may look like this:
{"cmd.exe", "/C", "Start", "program.bat", "stringA", "stringB", "stringC", stringD, stringE}
As you can see, there are two more strings added to the parameters list. My question is this: In my batch file I have this:
::Get stringA (param 1)
set stringA=%1
::Get stringB (param 2)
set stringB=%2
::Get stringC (param 3)
set stringC=%3
This takes the three string parameters (from the first "commands" array) and sets local variables to whatever values are passed in to the corresponding parameters. I am wondering if there is a way to determine the number of parameters (from the second "commands" array, for example) from within the batch file, and set/create the proper number of local variables accordingly. I focus primarily on Java, so batch files are still fairly new to me. Any suggestions will be very much appreciated, as I've been trying to figure this one out for a while on my own with no success.
#echo off
setlocal enabledelayedexpansion
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~x"
)
echo Number of processed arguments: %argCount%
for /L %%i in (1,1,%argCount%) do echo %%i- "!argVec[%%i]!"
For example:
C:> test One "This is | the & second one" Third
Number of processed arguments: 3
1- "One"
2- "This is | the & second one"
3- "Third"
Batch-Script - Iterate through arguments
As stated here:
https://stackoverflow.com/a/14298769/955143
You can read by %1 to %9, and use SHIFT to delete the first element from the array, an then renummerate they to new numbers.
For instance, the %2 becomes %1, and the 10th element becomes the 9th, and you can acess it by %9.
Since the read value become a empty string you have reached the end of the array
You can write a loop (with goto or for) to reade the %1 value, and use SHIFT to rotate the 2nd element to the first position, and check if it isn't empty, them repeat the process.

How to check if a batch passed parameter exists?

How could I modify the following example code to check if the input parameter was given when starting the batch file?
Because the check IF NOT %MYDIR%==test fails and terminates the batch process if no paramter was provided.
SET MYDIR=%1
IF {no parameter given} OR NOT %MYDIR%==test (
ECHO dir is not "test"
)
It is surprisingly difficult to handle all possibilities when dealing with passed parameters. But the following strategy works under most "ordinary" situations.
if "%~1" equ "" echo arg 1 was not passed
It is important that the ~ modifier is used because you have no way of knowing if the passed argument is already enclosed in quotes. If an argument like "this&that" is passed and you don't first remove the quotes before adding your own, then you get if ""this&that"" equ "". The & is no longer quoted and your command no longer parses properly.
Strings cannot be completely empty, a common way to work around this constraint is to enclose strings in quotes like this
... OR NOT "%MYDIR%"=="test"
or you can add something meaningless without enclosing the string (ugly!)
... OR NOT XXX%MYDIR%==XXXtest

How to send command line arguments to batch file containing "=" char

I want to invoke a batch file(tomcat's startup.bat) by passing a command line argument something like c:>startup.bat -Dsun.lang.ClassLoader.allowArraySyntax=true
But the "=" symbol is being replaced with a space.
If I put c:>startup.bat -D"sun.lang.ClassLoader.allowArraySyntax=true" the value was not set properly.
I am using Windows 7.
Is there anyway to pass command line arguments containing "="?
Thanks,
Siva
You can't do much about it inside the batch file, except change %1 to %1=%2, which only works if you know exactly how many parameters you're passing in, or you know they will always come in pairs. (I suppose you could loop and put together all of the -Dxxx parameters with the next parameter, and put those without a leading -D, but if you have other =-style parameters it can get really messy.)
But you can do something outside the batch file, by putting your parameter in quotes:
startup "-Dsun.lang.ClassLoader.allowArraySyntax=true"
You can use %* for all parameters.
In your batch
#echo off
javac %*
Or you can enquote your complete parameter
startup.bat "-Dsun.lang.ClassLoader.allowArraySyntax=true"
And startup.bat looks like (the %~1 removes surrounding quotes from %1)
#echo off
javac %~1

Cmd Line Arg like %1 and %s to get ALL args passed to program and not only the first

I have a program that deals with command line args being sent to the program by being executed by Run.
Sometimes I need to send more than one arg, and then the program crashes. And I noticed that it only takes the first one, and I know that it is a problem with the %1 or %s or %l that I use.
What is the right arg to send?
Assumption: batch scripting
Use %* to get all arguments.
%1 to %9 works for the first nine arguments. You can use shift to get those after %9.

Resources