问题描述
我有这样一行CMD文件TEST.CMD:
I have this single line CMD file TEST.CMD:
for %%f in (%1 %2 %3 %4 %5 %6 %7 %8) DO ECHO %%f
如果我运行此:
TEST this is a test
它正确回声在单独的行,即每个参数。
it correctly echos each parameter on a separate line, i.e.,
this
is
a
test
但是,如果一个参数包含星号它跳过它。如,
However if a parameter contains asterisk it skips it. E.g.,
TEST this is a* test
结果:
this
is
test
我如何获取参数用星号像一个正常的令牌可以治疗吗?
How do I get the parameter with an asterisk to be treated like a normal token?
感谢。
推荐答案
这对于大多数参数的工作原理是将参数传输给一个变量阵列,然后通过该阵列使用FOR / L至循环的最简单的方法。这是最好的延迟扩展来实现的。
The simplest method that works for most parameters is to transfer the parameters to an "array" of variables, and then use FOR /L to loop through the array. This is best achieved with delayed expansion.
此技术可以处理的参数的任意数目 - 它并不限于9
This technique can process an arbitrary number of parameters - it is not limited to 9.
@echo off
setlocal
:: Transfer parameters to an "array"
set arg.cnt=1
:getArgs
(set arg.%arg.cnt%=%1)
if defined arg.%arg.cnt% (
set /a arg.cnt+=1
shift /1
goto :getArgs
)
set /a arg.cnt-=1
:: Process the "array"
setlocal enableDelayedExpansion
for /l %%N in (1 1 %arg.cnt%) do echo arg %%N = !arg.%%N!
这篇关于批次有星号的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!