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

查看:44
本文介绍了在 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"=="" 这样的结构来完成的,当且仅当根本没有传递任何参数时才为真.注意波浪号字符,它导致从 %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%1 的值code>%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.

对于表示文件名的参数,shell 提供了许多与处理无法以任何其他方式访问的文件相关的功能.使用以 %~ 开头的结构访问此功能.

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天全站免登陆