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