如何使 SHIFT 与批处理文件中的 %* 一起使用
在 Windows XP 上的批处理文件中,我想使用 %*
扩展到除第一个参数之外的所有参数。
测试文件 (foo.bat< /em>):
@echo off
echo %*
shift
echo %*
调用:
C:\> foo a b c d e f
实际结果:
a b c d e f
a b c d e f
期望结果:
a b c d e f
b c d e f
我怎样才能达到期望的结果?谢谢!!
In my batch file on Windows XP, I want to use %*
to expand to all parameters except the first.
Test file (foo.bat):
@echo off
echo %*
shift
echo %*
Call:
C:\> foo a b c d e f
Actual result:
a b c d e f
a b c d e f
Desired result:
a b c d e f
b c d e f
How can I achieve the desired result? Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果 CMD.EXE 也能这样工作那该多好啊!不幸的是,没有一个好的语法可以满足您的要求。您能做的最好的事情就是自己解析命令行并构建一个新的参数列表。
像这样的东西可以工作。
但如果参数包含
^
、&
、>
、<
等特殊字符,上面的方法就会出现问题,|
被转义而不是引用。参数处理是 Windows 批处理编程的众多薄弱环节之一。几乎每个解决方案都存在导致问题的异常。
Wouldn't it be wonderful if CMD.EXE worked that way! Unfortunately there is not a good syntax that will do what you want. The best you can do is parse the command line yourself and build a new argument list.
Something like this can work.
But the above has problems if an argument contains special characters like
^
,&
,>
,<
,|
that were escaped instead of quoted.Argument handling is one of many weak aspects of Windows batch programming. For just about every solution, there exists an exception that causes problems.
这很简单:
与注释相同:
示例:
备注:
! 或
&
char%_args%
必须在endlocal
之前使用,因为它是本地的变量%_args%
返回* =
That´s easy:
Same thing with comments:
Example:
Remarks:
!
or&
char%_args%
must be used beforeendlocal
, because it is a local variable%_args%
returns* =
不要认为有简单的方法可以做到这一点。您可以尝试使用以下解决方法:
示例:
输出:
Don't think there's a simple way to do so. You could try playing with the following workaround instead:
Example:
Output:
我最近不得不这样做并想出了这个:
它使用循环来跳过
N
第一个参数。可用于为每个参数执行一些命令或构建新的参数列表:请注意,上面的代码将为新参数添加前导空格。可以像这样删除它:
I had to do this recently and came up with this:
It uses loop to skip over
N
first arguments. Can be used to execute some command per argument or build new argument list:Please note that the code above will add leading space to the new arguments. It can be removed like this:
您可以在调用另一个命令之前转移参数。
最新实现: https://github.com/andry81 /contools/tree/HEAD/Scripts/Tools/std/callshift.bat
优点:
%*
变量中的前 N 个使用的参数,包括额外的命令行参数。
线。
线路呼叫。
缺点:
&
和|
仍然必须在之前转义调用用户脚本(附带问题)。
cmd.exe /Q ...
可以完全抑制echo on
。setlocal DISABLEDELAYEDEXPANSION
,否则!
字符将被扩大了。
不同的编码器。
参数,每个参数必须至少包含一个空格
字符,因为所有制表字符都会编码,这可能会结束
加上参数串联以及错误的跳过和/或移位。
callshift.bat:
示例(在控制台中):
示例(在脚本中):
字符编码器和解码器:
encode_sys_chars_bat_cmdline.bat:
encode_sys_chars_exe_cmdline.bat:
decode_sys_chars_bat_cmdline.bat:
decode_sys_chars_exe_cmdline.bat:
encode_asterisk_char.bat:
encode_equal_char.bat:
You can shift arguments before call to another command.
Latest implementation: https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/callshift.bat
Pros:
%*
variable includingadditional command line arguments.
line.
line call.
Cons:
&
and|
still must be escaped beforecall in a user script (side issue).
cmd.exe /Q ...
can suppressecho on
at all.setlocal DISABLEDELAYEDEXPANSION
, otherwise the!
character will beexpanded.
different encoders.
argument, you must entail each argument at least with one space
character, because all tabulation characters does encode which may end
up with arguments concatenation and so wrong skip and/or shift.
callshift.bat:
Examples (in console):
Examples (in script):
Character encoders and decoders:
encode_sys_chars_bat_cmdline.bat:
encode_sys_chars_exe_cmdline.bat:
decode_sys_chars_bat_cmdline.bat:
decode_sys_chars_exe_cmdline.bat:
encode_asterisk_char.bat:
encode_equal_char.bat:
另一种简单的方法是:
备注:
Another easy way of doing this is:
Remarks:
DOS/Windows 批处理编程的另一个令人讨厌的缺点...
不确定这是否实际上比这里的其他一些答案更好,但我想我会分享一些似乎对我有用的东西。该解决方案使用 FOR 循环而不是 goto,并且包含在可重用的批处理脚本中。
单独的批处理脚本,“shiftn.bat”:
如何在另一个批处理脚本中使用shiftn.bat;在此示例中,获取第一个(跳过的)参数之后的所有参数:
也许其他人可以利用此解决方案的某些方面(或提供改进建议)。
Yet another obnoxious shortcoming of DOS/Windows batch programming...
Not sure if this is actually better than some of the other answers here but thought I'd share something that seems to be working for me. This solution uses FOR loops rather than goto, and is contained in a reusable batch script.
Separate batch script, "shiftn.bat":
How to use shiftn.bat in another batch script; in this example getting all arguments following the first (skipped) arg:
Perhaps someone else can make use of some aspects of this solution (or offer suggestions for improvement).
恢复所有并修复所有问题:
参数之间不支持空格:
1 2 3 4
将是1 2 3 4
Resume of all and fix all problems:
Unsupport spaces between arguments:
1 2 3 4
will be1 2 3 4