传递命令参数批处理

时间:2013-04-25 21:42:48

标签: batch-file cmd

我需要使用批处理文件传递命令参数。我该怎么办?我有一个小程序设置,但我不知道如何获取命令参数..或者真的如何发出命令。

@echo OFF
title Name pl0x
color 0a
:start
set INPUT=
set /P INPUT= %=%
IF "%INPUT%"=="/mynameis" (
goto :init
) ELSE (
goto :start
)
:init
Pause&Exit
REM Here, I'd print the name, like this: echo.Hello, %name%
REM I just don't have the variable. And I don't know how to set it.

2 个答案:

答案 0 :(得分:2)

使用%1%2等进行参数

所以如果你有mynameis.bat%1将包含test

答案 1 :(得分:1)

参数是一个空格分隔的单词,可以像应用程序中的参数一样使用。

然后如果调用这样的脚本:

Script.Bat word1 word2 word3_word4-word5

参数是:

Argument 1 = "word1"
Argument 2 = "word2"
Argument 3 = "word3_word4-word5" (Because any space).

批处理参数存储在从%1 %255 的特殊变量中,其中%1 等于“参数1”。< / p>

%0等于当前脚本名称或过程名称。

%*加入所有参数

..特殊修饰符扩展参数,不带双引号。

然后,如果我们有这个代码:

@Echo OFF
If /I "%~1" EQU "/Print" (Echo: You called the function "%~1" with the value: "%~2"))

我们正在检查参数1是否等于“/ Print”,如果是等于打印开关的值,则是第二个参数。

然后你可以像这样打电话给你的球棒:

Script.bat /Print Hello!

在此处详细了解批处理参数:http://ss64.com/nt/syntax-args.html

相关问题