有没有办法指示批处理文件中的最后 n 个参数? [英] Is there a way to indicate the last n parameters in a batch file?
问题描述
在以下示例中,我想从父批处理文件调用子批处理文件,并将所有剩余参数传递给子批处理文件.
In the following example, I want to call a child batch file from a parent batch file and pass all of the remaining parameters to the child.
C:> parent.cmd child1 foo bar
C:> parent.cmd child2 baz zoop
C:> parent.cmd child3 a b c d e f g h i j k l m n o p q r s t u v w x y z
在 parent.cmd 中,我需要从参数列表中去除 %1,并且只将剩余的参数传递给子脚本.
Inside parent.cmd, I need to strip %1 off the list of parameters and only pass the remaining parameters to the child script.
set CMD=%1
%CMD% <WHAT DO I PUT HERE>
我研究过将 SHIFT 与 %* 一起使用,但这不起作用.虽然 SHIFT 会将位置参数向下移动 1,但 %* 仍指原始参数.
I've investigated using SHIFT with %*, but that doesn't work. While SHIFT will move the positional parameters down by 1, %* still refers to the original parameters.
有人有什么想法吗?我应该放弃并安装 Linux 吗?
Anyone have any ideas? Should I just give up and install Linux?
推荐答案
%*
将始终扩展到所有原始参数,遗憾的是.但是您可以使用以下代码片段来构建一个包含除第一个参数之外的所有参数的变量:
%*
will always expand to all original parameters, sadly. But you can use the following snippet of code to build a variable containing all but the first parameter:
rem throw the first parameter away
shift
set params=%1
:loop
shift
if [%1]==[] goto afterloop
set params=%params% %1
goto loop
:afterloop
我认为它可以做得更短,不过......我不经常写这些东西:)
I think it can be done shorter, though ... I don't write these sort of things very often :)
不过应该可以.
这篇关于有没有办法指示批处理文件中的最后 n 个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!