我想找到一个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
其他回答
%*似乎包含传递给脚本的所有参数。
有时我需要提取几个命令行参数,但不是全部,也不是从第一个参数提取。让我们假设下面的调用:
Test.bat uno dos tres cuatro cinco seis siete
当然,扩展名“。bat”是不需要的,至少你在同一个文件夹中有test.exe。我们想要的是得到输出“tres cuatro cinco”(不管是一行还是三行)。我们的目标是只需要三个参数,并从第三个参数开始。为了有一个简单的例子,每个操作都是“echo”,但是你可以考虑其他一些更复杂的操作。
我提供了一些例子,(选项),以便你可以看看你认为哪个更适合你。不幸的是,没有一个好的(或者我不知道)方法基于范围内的for循环,比如“for %% I in(%3, 1, %5)”。
@echo off
setlocal EnableDelayedExpansion
echo Option 1: one by one (same line)
echo %3, %4, %5
echo.
echo Option 2: Loop For one by one
for %%a in (%3, %4, %5) do echo %%a
echo.
echo Option 3: Loop For with check of limits
set i=0
for %%a in (%*) do (
set /A i=i+1
If !i! GTR 2 if !i! LSS 6 echo %%a
)
echo.
echo Option 4: Loop For with auxiliary list
for /l %%i in (3,1,5) do (
set a=%%i
set b=echo %%
set b=!b!!a!
call !b!
)
echo.
echo Option 5: Assigning to an array of elements previously
set e[0]=%0
set i=0
for %%a in (%*) do (
set /A i=i+1
set e[!i!]=%%a
)
for /l %%i in (3,1,5) do (
echo !e[%%i]!
)
echo.
echo Option 6: using shift and goto loop. It doesn't work with for loop
set i=2
:loop6
set /A i=i+1
echo %3
shift
If %i% LSS 5 goto :loop6
也许你可以找到更多的选项,或者将几个选项结合起来。享受其中的乐趣。
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
我发现下次你需要查这些资料的时候。你不需要打开浏览器,只需要输入call /?在你的CMD,你会得到它:
...
%* in a batch script refers to all the arguments (e.g. %1 %2 %3
%4 %5 ...)
Substitution of batch parameters (%n) has been enhanced. You can
now use the following optional syntax:
%~1 - expands %1 removing any surrounding quotes (")
%~f1 - expands %1 to a fully qualified path name
%~d1 - expands %1 to a drive letter only
%~p1 - expands %1 to a path only
%~n1 - expands %1 to a file name only
%~x1 - expands %1 to a file extension only
%~s1 - expanded path contains short names only
%~a1 - expands %1 to file attributes
%~t1 - expands %1 to date/time of file
%~z1 - expands %1 to size of file
%~$PATH:1 - searches the directories listed in the PATH
environment variable and expands %1 to the fully
qualified name of the first one found. If the
environment variable name is not defined or the
file is not found by the search, then this
modifier expands to the empty string
The modifiers can be combined to get compound results:
%~dp1 - expands %1 to a drive letter and path only
%~nx1 - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
environment variable for %1 and expands to the
drive letter and path of the first one found.
%~ftza1 - expands %1 to a DIR like output line
In the above examples %1 and PATH can be replaced by other
valid values. The %~ syntax is terminated by a valid argument
number. The %~ modifiers may not be used with %*
你可以使用命令,以获得参数列表。
帮助:For /? 帮助:Setlocal /?
这是我的方法=
@echo off
::For Run Use This = cmd /c ""Args.cmd" Hello USER Scientist etc"
setlocal EnableDelayedExpansion
set /a Count=0
for %%I IN (%*) DO (
Echo Arg_!Count! = %%I
set /a Count+=1
)
Echo Count Of Args = !Count!
Endlocal
不需要Shift命令。