我想找到一个Windows批处理对应Bash的$@,它持有传递到脚本的所有参数的列表。
还是要我费心去换班?
我想找到一个Windows批处理对应Bash的$@,它持有传递到脚本的所有参数的列表。
还是要我费心去换班?
当前回答
对于使用循环获取所有参数,并且是纯批处理:
注意事项:用于不带:?*&<>
@echo off && setlocal EnableDelayedExpansion
for %%Z in (%*)do set "_arg_=%%Z" && set/a "_cnt+=1+0" && call set "_arg_[!_cnt!]=!_arg_!")
:: write/test these arguments/parameters ::
for /l %%l in (1 1 !_cnt!)do echo/ The argument n:%%l is: !_arg_[%%l]!
goto :eof
你的代码已经准备好在需要的地方用参数号做一些事情,比如……
@echo off && setlocal EnableDelayedExpansion
for %%Z in (%*)do set "_arg_=%%Z" && set/a "_cnt+=1+0" && call set "_arg_[!_cnt!]=!_arg_!"
echo= !_arg_[1]! !_arg_[2]! !_arg_[2]!> log.txt
其他回答
获取脚本中所有参数的方法如下:
@ECHO off
ECHO The %~nx0 script args are...
for %%I IN (%*) DO ECHO %%I
pause
@echo off :start :: Insert your code here echo.%%1 is now:%~1 :: End insert your code here if "%~2" NEQ "" ( shift goto :start )
%*似乎包含传递给脚本的所有参数。
上面的很多信息引导我进行了进一步的研究,最终得出了我的答案,所以我想贡献我所做的事情,希望它能帮助其他人:
我还想向批处理文件传递不同数量的变量,以便在文件中处理它们。 我可以使用引号将它们传递给批处理文件 我希望他们处理类似于下面-但使用循环而不是手动写入:
所以我想执行这个:
prog_ZipDeleteFiles.bat "_appPath=C:\Services\Logs\PCAP" "_appFile=PCAP*.?"
通过for循环的魔力在批处理文件中做到这一点:
set "_appPath=C:\Services\Logs\PCAP"
set "_appFile=PCAP*.?"
我遇到的问题是,我使用for循环的所有尝试都不起作用。下面的例子:
for /f "tokens* delims= " in %%A (%*) DO (
set %%A
)
会这样做:
set "_appPath=C:\Services\Logs\PCAP"
而不是:
set "_appPath=C:\Services\Logs\PCAP"
set "_appFile=PCAP*.?"
即使在设置之后
SETLOCAL EnableDelayedExpansion
原因是for循环读取了整行,并在循环的第一次迭代中将第二个参数赋值给%%B。因为%*表示所有的参数,所以只需要处理一行——因此只会发生一次for循环。事实证明,这是故意的,而我的逻辑是错误的。
因此,我不再尝试使用for循环,而是通过使用if、shift和goto语句来简化我试图做的事情。同意,这有点黑,但它更适合我的需要。我可以遍历所有参数,然后在处理完所有参数后使用if语句退出循环。
我想要实现的目标是:
echo on
:processArguments
:: Process all arguments in the order received
if defined %1 then (
set %1
shift
goto:processArguments
) ELSE (
echo off
)
更新-我不得不修改如下,当我试图引用%1并以这种方式使用shift时,我暴露了所有的环境变量:
echo on
shift
:processArguments
:: Process all arguments in the order received
if defined %0 then (
set %0
shift
goto:processArguments
) ELSE (
echo off
)
Dancavallaro是正确的,%*用于所有命令行参数(不包括脚本名称本身)。你可能还会发现这些有用:
%0 -用于调用批处理文件的命令(可以是foo, ..\foo, c:\bats\foo.bat等) %1是第一个命令行参数, %2是第二个命令行参数, 等等,直到%9 (SHIFT可以用于9号之后的)。
%~nx0—批处理文件的实际名称,与调用方法无关(some-batch.bat) %~dp0 -脚本的驱动器和路径(d:\scripts) %~dpnx0 -是脚本的完全限定路径名(d:\scripts\some-batch.bat)
更多信息的例子在https://www.ss64.com/nt/syntax-args.html和https://www.robvanderwoude.com/parameters.html