Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass arguments to a batch file?

I need to pass an ID and a password to a batch file at the time of running rather than hardcoding them into the file.

Here's what the command line looks like:

test.cmd admin P@55w0rd > test-log.txt 
like image 927
Keng Avatar asked Aug 25 '08 18:08

Keng


People also ask

How do I pass parameters to a batch file?

In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.

How many arguments can be passed to a 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 is %% A in batch script?

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.

How do I pass a command line argument in command prompt?

Note : You pass all the command line arguments separated by a space, but if argument itself has a space then you can pass such arguments by putting them inside double quotes “” or single quotes ”.


1 Answers

Another useful tip is to use %* to mean "all". For example:

echo off set arg1=%1 set arg2=%2 shift shift fake-command /u %arg1% /p %arg2% %* 

When you run:

test-command admin password foo bar 

the above batch file will run:

fake-command /u admin /p password admin password foo bar 

I may have the syntax slightly wrong, but this is the general idea.

like image 95
Greg Hewgill Avatar answered Oct 06 '22 12:10

Greg Hewgill