在使用批处理文件的参数在DOS命令行 [英] Using parameters in batch files at DOS command line

查看:199
本文介绍了在使用批处理文件的参数在DOS命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows中,你如何访问传递的参数时,一个批处理文件运行?

In Windows, how do you access arguments passed when a batch file is run?

例如,假设我有一个名为程序 hello.bat 。当我输入你好-a 在DOS命令行中,我该如何让我的程序知道 -a 传递作为参数?

For example, let's say I have a program named hello.bat. When I enter hello -a at a DOS command line, how do I let my program know that -a was passed in as an argument?

推荐答案

正如其他人已经说过,通过命令行传递的参数可以与符号批处理文件来访问%1 %9 。还有其他两个令牌,你可以使用:

As others have already said, parameters passed through the command line can be accessed in batch files with the notation %1 to %9. There are also two other tokens that you can use:


  • 0%在命令行中指定的可执行文件(批处理文件)名称

  • %* 在命令行指定的所有参数 - 如果你想要的参数转发到另一个程序,这是非常有用的。

  • %0 is the executable (batch file) name as specified in the command line.
  • %* is all parameters specified in the command line -- this is very useful if you want to forward the parameters to another program.

有也很多重要的技术要意识到的,除了简单地如何访问参数

There are also lots of important techniques to be aware of in addition to simply how to access the parameters.

这是与像结构做了IF%〜1==,当且仅当没有论据都通过了这是事实。注意波形符这会从%1 的值删除任何引号;没有一个波浪线,你会得到意想不到的结果,如果该值包含双引号,包括语法错误的可能性。

This is done with constructs like IF "%~1"=="", which is true if and only if no arguments were passed at all. Note the tilde character which causes any surrounding quotes to be removed from the value of %1; without a tilde you will get unexpected results if that value includes double quotes, including the possibility of syntax errors.

如果您需要访问超过900的参数,你必须使用命令 SHIFT 。此命令转移的所有参数的值一处,使%0 需要%1 ,<$值C $ C>%1 需要%2 的值等%9 采用第十参数的值(如果有的话present),这是无法通过任何变量调用 SHIFT (输入命令 SHIFT前/?更多选项)。

If you need to access more than 9 arguments you have to use the command SHIFT. This command shifts the values of all arguments one place, so that %0 takes the value of %1, %1 takes the value of %2, etc. %9 takes the value of the tenth argument (if one is present), which was not available through any variable before calling SHIFT (enter command SHIFT /? for more options).

SHIFT 也很有用。例如,一个脚本可以识别任意顺序标志 -a -b 。解析命令行在这种情况下的一个好办法就是

SHIFT is also useful when you want to easily process parameters without requiring that they are presented in a specific order. For example, a script may recognize the flags -a and -b in any order. A good way to parse the command line in such cases is

:parse
IF "%~1"=="" GOTO endparse
IF "%~1"=="-a" REM do something
IF "%~1"=="-b" REM do something else
SHIFT
GOTO parse
:endparse
REM ready for action!

这个方案允许您解析pretty复杂的命令行而不去疯了。

This scheme allows you to parse pretty complex command lines without going insane.

对于重新present文件名壳提供了大量的功能相关,与不以任何其他方式访问的文件工作参数。此功能与以%〜构造访问。

For parameters that represent file names the shell provides lots of functionality related to working with files that is not accessible in any other way. This functionality is accessed with constructs that begin with %~.

例如,要获得传递作为参数使用文件的大小

For example, to get the size of the file passed in as an argument use

ECHO %~z1

要获得这个批文件是从启动目录的路径(非常有用!),你可以使用

To get the path of the directory where the batch file was launched from (very useful!) you can use

ECHO %~dp0

您可以通过在命令提示符下键入 CALL /?查看全方位的这些功能​​。

You can view the full range of these capabilities by typing CALL /? in the command prompt.

这篇关于在使用批处理文件的参数在DOS命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆