Russ Romei
Russ Romei

Reputation: 11

passing parameters to bat file as 1 string

It seems like it would be simple. In a bat file, I call a program that takes command line parameters I have the parms as one string, I have tried it with "" around and not It only sees one parm (if guess it hit the space)

So the question is how can I pass the string, as a parameter list The actual string is below and must be formated as such with the "" etc

-U romeirj -P Abc123 -F C:\inetpub\wwwroot\russ\crexport\russ.rpt -O C:\inetpub\wwwroot\russ\russ.pdf -A "startdate: 01-01-2010" -A "gender:M" -A "type:PENDJUD"

would like to call it from a bat file the looks like

batfile.bat parmstring

bat file content

program.exe %1

Upvotes: 1

Views: 3922

Answers (1)

dbenham
dbenham

Reputation: 130919

As long as all of the batch parameters are supposed to be passed to your program, then you can simply call your batch with the parameters as you have specified them, and use the following within your batch script.

program.exe %*

The problem becomes much more complicated if you only want to pass some of the batch parameters to the called program.

Unfortunately there is no method to escape quotes within a quoted string. It is also impossible to escape parameter delimiters. So it is impossible to simultaneously embed both spaces and quotes within a single batch parameter.

The SHIFT command can strip off leading parameters, but %* always expands to the original parameter list; it ignores prior SHIFT operations.

The FOR /F does not ignore quoted delimiters, so it doesn't help.

The simple FOR can properly parse quoted parameter lists, but it expands * and ? characters using the file system. That can be a problem.

The only thing left to do is to use a GOTO loop combined with SHIFT to build a string containing your desired parameters.

Suppose the first 3 parameters are strictly for the batch file, and the remaining parameters are to be passed to the called program.

@echo off
setlocal

set "args="
:buildProgramArgs
if [%4]==[] goto :argsComplete
set args=%args% %4
shift /4
goto :buildProgramArgs

:argsComplete
program.exe %args%

::args %1 %2 and %3 are still available for batch use

Upvotes: 3

Related Questions