Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple params in batch?

Tags:

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?

like image 450
IAdapter Avatar asked Feb 02 '11 07:02

IAdapter


People also ask

How many parameters can I pass to batch file?

There is no practical limit to the number of parameters you can pass to a batch file, but you can only address parameter 0 (%0 - The batch file name) through parameter 9 (%9).

What does %% mean in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

Can we pass parameters to batch file?

You simply substitute the parameter for 1 (e.g., %~f2 for the second parameter's fully qualified path name). The %0 parameter in a batch file holds information about the file when it runs and indicates which command extensions you can use with the file (e.g., %~dp0 gives the batch file's drive and path).

What does %1 do in batch?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


2 Answers

There is a magic way! I knew it, but I could not remember it.

its %*

like image 157
IAdapter Avatar answered Oct 12 '22 10:10

IAdapter


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.

like image 40
Simon Campbell Avatar answered Oct 12 '22 10:10

Simon Campbell