How do I pass command line parameters to a batch file?

I need 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 what the command line looks like:

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

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 foo bar

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


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.


如果你想聪明地处理缺少的参数,你可以做如下的事情:

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
链接地址: http://www.djcxy.com/p/5124.html

上一篇: 如何找出在Windows上的端口上侦听哪个进程?

下一篇: 如何将命令行参数传递给批处理文件?