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

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

问题描述

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

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

例如,假设我有一个名为hello.bat的程序.在Windows命令行上输入hello -a时,如何让程序知道-a作为参数传递?

For example, let's say I have a program named hello.bat. When I enter hello -a at a Windows 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"==""这样的结构完成的,当且仅当根本不传递任何参数时,它才为true.请注意,代字号字符会导致所有周围的引号从%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.

如果需要访问9个以上的参数,则必须使用命令SHIFT.此命令将所有参数的值移动一个位置,以使%0取值为%1%1取值为%2,依此类推.%9取第十个参数的值(如果一个(存在)),在调用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!

此方案使您可以解析非常复杂的命令行,而不会发疯.

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

对于表示文件名的参数,外壳程序提供了许多与处理文件相关的功能,这些功能无法通过其他任何方式访问.可使用以%~开头的构造访问此功能.

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.

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

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