我想找到一个Windows批处理对应Bash的$@,它持有传递到脚本的所有参数的列表。

还是要我费心去换班?


当前回答

% 1…%n和%*保存参数,但是访问它们可能很棘手,因为内容将被解释。 因此,用普通语句处理这样的事情是不可能的

myBatch.bat "&"^&

每一行都失败,因为cmd.exe试图执行一个&号(%1的内容是“&”&)

set var=%1
set "var=%1"
set var=%~1
set "var=%~1"

但是存在一个临时文件的解决方案

@echo off
SETLOCAL DisableDelayedExpansion

SETLOCAL
for %%a in (1) do (
    set "prompt=$_"
    echo on
    for %%b in (1) do rem * #%1#
    @echo off
) > param.txt
ENDLOCAL

for /F "delims=" %%L in (param.txt) do (
  set "param1=%%L"
)
SETLOCAL EnableDelayedExpansion
set "param1=!param1:*#=!"
set "param1=!param1:~0,-2!"
echo %%1 is '!param1!'

诀窍是在rem语句后启用echo并展开%1(也适用于%2 ..% *)。 但是为了能够重定向echo on的输出,您需要两个for - loop。

额外的字符* #用于安全对抗/?(有助于快速眼动)。 或者行尾的插入符号^可以作为多行字符。

FOR /F应关闭延迟展开,否则包含“!”的内容将被销毁。 在删除param1中的额外字符后,您就得到了它。

要以安全的方式使用param1,请启用延迟扩展。

编辑:一条注释到%0

%0包含用于调用批处理的命令,也像FoO.BaT中一样保留大小写 但是在调用函数%0之后,也在%~0中包含函数名(或者更好的是用于调用函数的字符串)。 但是使用%~f0,您仍然可以找回文件名。

@echo off
echo main %0, %~0, %~f0
call :myLabel+xyz
exit /b

:MYlabel
echo func %0, %~0, %~f0
exit /b

输出

main test.bat, test.bat, C:\temp\test.bat
func :myLabel+xyz, :myLabel+xyz, C:\temp\test.bat

其他回答

%*似乎包含传递给脚本的所有参数。

我发现下次你需要查这些资料的时候。你不需要打开浏览器,只需要输入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 %*

下面的代码模拟了一个数组('params')——接受脚本接收到的参数,并将它们存储在变量params_1 ..Params_n,其中n=params_0=数组元素的个数:

@echo off

rem Storing the program parameters into the array 'params':
rem Delayed expansion is left disabled in order not to interpret "!" in program parameters' values;
rem however, if a parameter is not quoted, special characters in it (like "^", "&", "|") get interpreted at program launch
set /a count=0
:repeat
    set /a count+=1
    set "params_%count%=%~1"
    shift
    if defined params_%count% (
        goto :repeat
    ) else (
        set /a count-=1
    )    
set /a params_0=count

rem Printing the program parameters stored in the array 'params':
rem After the variables params_1 .. params_n are set with the program parameters' values, delayed expansion can
rem be enabled and "!" are not interpreted in the variables params_1 .. params_n values
setlocal enabledelayedexpansion
    for /l %%i in (1,1,!params_0!) do (
        echo params_%%i: "!params_%%i!"
    )
endlocal

pause
goto :eof

如果参数在包含空格的引号中,%*将无法正确工作。 我发现的最佳解决方案是使用一个连接所有参数的循环:https://serverfault.com/a/22541

set args=%1
shift
:start
if [%1] == [] goto done
set args=%args% %1
shift
goto start

:done
(use %args% here)

获取脚本中所有参数的方法如下:

@ECHO off
ECHO The %~nx0 script args are...
for %%I IN (%*) DO ECHO %%I
pause