在批处理文件中使用参数:%0 和 %9
批处理文件可以通过标记引用作为参数传入的单词:%0 到 %9。
%0 is the program name as it was called.
%1 is the first command line parameter
%2 is the second command line parameter
and so on till %9.
在命令行中传入的参数必须是字母数字字符并以空格分隔。由于%0 是调用时的程序名称,因此在 DOS 中%0 如果在引导时启动,则 AUTOEXEC.BAT 将为空。
示例:
将以下命令放入名为mybatch.bat 的批处理文件中:
@echo off
@echo hello %1 %2
pause
像这样调用批处理文件:mybatch john billy 将输出:
hello john billy
为一个批处理文件获取超过 9 个参数,使用:%*
百分比星标记%* 表示“其余参数”。您可以使用 for 循环来获取它们,定义如下:
http://www.robvanderwoude.com/parameters.php
关于批处理参数分隔符的说明
命令行参数中的某些字符会被批处理文件忽略,具体取决于 DOS 版本,是否“转义”,并且通常取决于它们在命令行中的位置:
commas (",") are replaced by spaces, unless they are part of a string in
double quotes
semicolons (";") are replaced by spaces, unless they are part of a string in
double quotes
"=" characters are sometimes replaced by spaces, not if they are part of a
string in double quotes
the first forward slash ("/") is replaced by a space only if it immediately
follows the command, without a leading space
multiple spaces are replaced by a single space, unless they are part of a
string in double quotes
tabs are replaced by a single space
leading spaces before the first command line argument are ignored