views:

40665

answers:

5

I needed to pass id and password to a cmd (or bat) file at the time of running rather than hardcoding them into the file.

Here's how I do it.

echo off
fake-command /u %1 /p %2

Here's what the command line looks like:

test.cmd admin P@55w0rd > test-log.txt

The %1 applies to the first parameter the %2 (and here's the tricky part) applies to the second. You can have up to 9 parameters passed in this way.

Afterward: This is my first attempt to answer my own question which, to hear Jeff discuss it is a "...perfectly acceptable...." way of using SO. I'm just not certain if there's already a format for doing it.


Additional Notes from the Community

Per Frank Krueger

Yep, and just don't forget to use variables like %%1 when using if and for and the gang.

If you forget the double %, then you will be substituting in (possibly null) command >line arguments and you will receive some pretty confusing error messages.

Per Greg Hewgill

Another useful tip is to use %* to mean "all the rest". For example,

   echo off
   fake-command /u %1 /p %2 %*

When you run:

   test-command admin password foo bar

the above batch file will run:

   fake-command /u admin /p password foo bar

Edit: Actually, %* means "all", so one would actually need to use shift to do the above:

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

I may still have the syntax slightly wrong, but this is the general idea. It's been a >very long time since I've written a batch file, and my brain keeps thinking "shell script"!

Per thelsdj

If you want to intelligently handle missing parameters you can do something like:

   IF %1.==. GOTO No1
   IF %2.==. GOTO No2
   ... do stuff...
   GOTO End1
   :No1
   ECHO No param 1
   GOTO End1
   :No2
   ECHO No param 2
   GOTO End1
   :End1
+4  A: 

Yep, and just don't forget to use variables like %%1 when using if and for and the gang.

If you forget the double %, then you will be substituting in (possibly null) command line arguments and you will receive some pretty confusing error messages.

Frank Krueger
+9  A: 

Another useful tip is to use %* to mean "all the rest". For example,

echo off
fake-command /u %1 /p %2 %*

When you run:

test-command admin password foo bar

the above batch file will run:

fake-command /u admin /p password foo bar

Edit: Actually, %* means "all", so one would actually need to use shift to do the above:

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

I may still have the syntax slightly wrong, but this is the general idea. It's been a very long time since I've written a batch file, and my brain keeps thinking "shell script"!

Greg Hewgill
%* actually expands to all parameters regardless of shift. So even after the two shifts you would still have the first two arguments in %*. You can use something like this: http://stackoverflow.com/questions/761615/is-there-a-way-to-indicate-the-last-n-parameters-in-a-batch-file/761658#761658 to get a variable that contains everything but the first n parameters.
Joey
Please note that %* does not work everywhere! For instance, it does not work with DOSBox 0.73 (maybe this is a bug that should be reported).
Denilson Sá
+7  A: 

If you want to intelligently handle missing parameters you can do something like:

IF %1.==. GOTO No1
IF %2.==. GOTO No2
... do stuff...
GOTO End1

:No1
  ECHO No param 1
GOTO End1
:No2
  ECHO No param 2
GOTO End1

:End1
thelsdj
+2  A: 

Here's my answer in the new correct 'answer your own question' format.


Here's how I do it.

fake-command /u %1 /p %2

Here's what the command line looks like:

test.cmd admin P@55w0rd > test-log.txt

The %1 applies to the first parameter the %2 (and here's the tricky part) applies to the second. You can have up to 9 parameters passed in this way.

Keng
A: 

For "all the rest" (Greg Hegill's comment) see how to get batch file parameters from the Nth position on

matt wilkie