【发布时间】:2008-12-10 19:38:38
【问题描述】:
我想找到一个与 Bash 的 $@ 对应的 Windows 批处理,它包含传递给脚本的所有参数的列表。
或者我不得不打扰shift?
【问题讨论】:
标签: windows batch-file
我想找到一个与 Bash 的 $@ 对应的 Windows 批处理,它包含传递给脚本的所有参数的列表。
或者我不得不打扰shift?
【问题讨论】:
标签: windows batch-file
dancavallaro 是正确的,%* 用于所有命令行参数(不包括脚本名称本身)。您可能还会发现这些有用:
%0 - 用于调用批处理文件的命令(可以是foo、..\foo、c:\bats\foo.bat等)%1是第一个命令行参数,
@ 987654330@是第二个命令行参数,
依此类推,直到%9(SHIFT 可用于 9 号之后的人)。
%~nx0 - 批处理文件的实际名称,与调用方法无关 (some-batch.bat)%~dp0 - 脚本的驱动器和路径 (d:\scripts)
@987654335 @ - 是脚本的完全限定路径名 (d:\scripts\some-batch.bat)
https://www.ss64.com/nt/syntax-args.html 和 https://www.robvanderwoude.com/parameters.html 上的更多信息示例
【讨论】:
%~dpn0 将返回脚本的驱动器和路径以及批处理文件的名称,但不返回扩展名。在创建调用具有相同名称的.ps1 文件的批处理脚本时,我发现这特别有用。本质上,d 指的是驱动器,p 指的是路径,n 指的是文件名,x 指的是扩展名。有了这些信息,您可以做很多事情。
@echo %~n0.my_ext
%* 似乎包含传递给脚本的所有参数。
【讨论】:
&?当我尝试检索几个选定文件的路径时(使用发送到菜单和将参数传递给 .py 的“%*”),它会在 & 字符处停止列表。因此,例如,如果第二个文件的名称中包含 &,则将跳过 & 之后的名称部分,以及剩余的文件。)
& 是一个特殊字符,表示stop this command and start another one。包含& 的文件名必须被引用。
我发现下次您需要查找这些信息时。您可以在 cmd 中输入 call /? ,而不是打开浏览器并搜索它,然后您就会得到它:
...
%* 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 %*
【讨论】:
%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!'
诀窍是启用echo on 并在rem 语句之后扩展%1(也适用于%2 .. %*)。
但是为了能够重定向echo on 的输出,您需要两个FOR-LOOPS。
额外的字符* # 用于防止/? 之类的内容(将显示REM 的帮助)。
或者行尾的插入符号 ^ 可以用作多行字符。
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
【讨论】:
%~f0 技巧完全出乎意料,非常酷,而且可能非常有用:-) 毫不奇怪,其他修饰符的工作方式相同,总是在父批次上运行文件,即使在被调用的子程序中。这似乎值得它自己的问答——“在被调用的子例程中如何访问正在运行的批处理的名称?”
将所有参数检索到脚本的方法在这里:
@ECHO off
ECHO The %~nx0 script args are...
for %%I IN (%*) DO ECHO %%I
pause
【讨论】:
%%I 会被解释并可能会破坏您的脚本。
为了使用循环来获取所有参数和纯批处理:
Obs: 不使用:?*&<>
@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
【讨论】:
如果引号中的参数包含空格,%* 将无法正常工作。
我发现的最佳解决方案是有一个连接所有参数的循环: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)
【讨论】:
您可以使用For Commad,获取Arg列表。
帮助: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 命令。
【讨论】:
这是获取参数并将它们设置为环境变量的一种相当简单的方法。在本例中,我将它们称为键和值。
将以下代码示例另存为“args.bat”。然后调用批处理文件 您从命令行保存。 示例:arg.bat --x 90 --y 120
我提供了一些 echo 命令来引导您完成整个过程。但是 最终结果是 --x 的值为 90,而 --y 的值为 120(也就是说,如果您按照上面指定的方式运行示例 ;-))。
然后您可以使用“如果已定义”条件语句来确定是否运行您的代码块。 所以让我们说运行: “arg.bat --x 你好世界” 然后我可以使用语句“IF DEFINED --x echo %--x%”,结果将是“hello-world”。如果您运行批处理应该更有意义。
@setlocal enableextensions enabledelayedexpansion
@ECHO off
ECHO.
ECHO :::::::::::::::::::::::::: arg.bat example :::::::::::::::::::::::::::::::
ECHO :: By: User2631477, 2013-07-29 ::
ECHO :: Version: 1.0 ::
ECHO :: Purpose: Checks the args passed to the batch. ::
ECHO :: ::
ECHO :: Start by gathering all the args with the %%* in a for loop. ::
ECHO :: ::
ECHO :: Now we use a 'for' loop to search for our keys which are identified ::
ECHO :: by the text '--'. The function then sets the --arg ^= to the next ::
ECHO :: arg. "CALL:Function_GetValue" ^<search for --^> ^<each arg^> ::
ECHO :: ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO :: From the command line you could pass... arg.bat --x 90 --y 220 ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
ECHO.Checking Args:"%*"
FOR %%a IN (%*) do (
CALL:Function_GetValue "--","%%a"
)
ECHO.
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO :: Now lets check which args were set to variables... ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO :: For this we are using the CALL:Function_Show_Defined "--x,--y,--z" ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
CALL:Function_Show_Defined "--x,--y,--z"
endlocal
goto done
:Function_GetValue
REM First we use find string to locate and search for the text.
echo.%~2 | findstr /C:"%~1" 1>nul
REM Next we check the errorlevel return to see if it contains a key or a value
REM and set the appropriate action.
if not errorlevel 1 (
SET KEY=%~2
) ELSE (
SET VALUE=%~2
)
IF DEFINED VALUE (
SET %KEY%=%~2
ECHO.
ECHO ::::::::::::::::::::::::: %~0 ::::::::::::::::::::::::::::::
ECHO :: The KEY:'%KEY%' is now set to the VALUE:'%VALUE%' ::
ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
ECHO %KEY%=%~2
ECHO.
REM It's important to clear the definitions for the key and value in order to
REM search for the next key value set.
SET KEY=
SET VALUE=
)
GOTO:EOF
:Function_Show_Defined
ECHO.
ECHO ::::::::::::::::::: %~0 ::::::::::::::::::::::::::::::::
ECHO :: Checks which args were defined i.e. %~2
ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
SET ARGS=%~1
for %%s in (%ARGS%) DO (
ECHO.
ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO :: For the ARG: '%%s'
IF DEFINED %%s (
ECHO :: Defined as: '%%s=!%%s!'
) else (
ECHO :: Not Defined '%%s' and thus has no value.
)
ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
)
goto:EOF
:done
【讨论】:
【讨论】:
下面的代码模拟一个数组('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
【讨论】:
上面的很多信息让我进行了进一步的研究并最终得到了答案,所以我想贡献我最终所做的事情,希望它可以帮助其他人:
我还想将不同数量的变量传递给批处理文件,以便在文件中处理它们。
我可以使用引号将它们传递给批处理文件
我希望它们的处理方式类似于以下内容 - 但使用循环而不是手动写出:
所以我想执行这个:
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
)
【讨论】:
有时我需要提取几个命令行参数,但不是全部,也不是从第一个参数中提取。让我们假设以下调用:
Test.bat uno dos tres cuatro cinco seis siete
当然,扩展名“.bat”是不需要的,至少你在同一个文件夹中有 test.exe。我们想要的是获得输出“tres cuatro cinco”(无论是一行还是三行)。目标是我只需要三个参数并从第三个开始。为了有一个简单的例子,每个动作都只是“回声”,但您可以在所选参数上考虑其他更复杂的动作。
我制作了一些示例(选项),以便您了解您认为哪个更适合您。不幸的是,没有一种基于 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
也许您可以找到更多选项或将多个选项组合起来。尽情享受吧。
【讨论】: