[英]Batch script loop to check for multiple parameters
Ok, I keep playing with this but can't get it to run the command for each parameters.好的,我一直在玩这个,但无法让它为每个参数运行命令。
Batch file run as批处理文件运行方式
test.bat /r /a /c
Full Batch Code完整的批次代码
@echo on
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
:checkloop
set argtoken=1
FOR /F "Tokens=* delims=" %%G IN ("%*") DO (call :argcheck %%G)
pause
GOTO:END
:argcheck
if /i "%1"=="/r" set windows=1
if /i "%1"=="/a" set active=1
goto:eof
:end
"%*" Displays all of the arguments such as "%*" 显示所有参数,例如
/r /a /c
But for some reason no matter what I try, I can't get the for loop to break up different parameters and run :argcheck
for each parameter.但是由于某种原因,无论我尝试什么,我都无法让 for 循环分解不同的参数并为每个参数运行
:argcheck
。
UPDATE: For anyone interested here is what I ended up wtih.更新:对于任何对这里感兴趣的人来说,我最终都会这样做。 I am implementing it in a few different scripts and its working amazing.
我在几个不同的脚本中实现它,它的工作令人惊叹。 Just place it somewhere in the script with the call function and the "%*" and you should be good.
只需将它放在带有调用函数和“%”的脚本中的某个位置,你应该会很好。 :) Post if you have any problems with it.
:) 如果您有任何问题,请发布。
@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
call:ArgumentCheck %*
echo %DebugMode%
echo %RestartAfterInstall%
pause
goto:eof
:ArgumentCheck
if "%~1" NEQ "" (
if /i "%1"=="/r" SET DebugMode=Yes & GOTO:ArgumentCheck_Shift
if /i "%1"=="/a" SET RestartAfterInstall=Yes & GOTO:ArgumentCheck_Shift
SET ArgumentCheck_Help=Yes
:ArgumentCheck_Shift
SHIFT
goto :ArgumentCheck
)
If "%ArgumentCheck_Help%"=="Yes" (
Echo An invalid argument has been passed, currently this script only supports
ECHO /r /a arguments. The script will continue with the arguments
ECHO you passed that is supported.
)
GOTO:EOF
:end
The cause is that FOR/F will split one line into a fixed count of tokens named %%A,%%B,%%... (%%A is here the first named token).原因是 FOR/F 会将一行拆分为固定数量的令牌,名为 %%A、%%B、%%...(此处%%A 是第一个命名的令牌)。
But as you use empty delims=
even this will not work.但是当你使用空的
delims=
时,即使这样也行不通。
FOR /F "tokens=1-5 delims= " %%A in ("%*") do (
echo %%A, %%B, %%C, %%D, %%E
)
This would split your line into tokens, but it would even split tokens like这会将您的行拆分为令牌,但它甚至会将令牌拆分为
One "two and three"
Output:输出:
One, "two, and, three",
It's easier to use SHIFT
and a loop.使用
SHIFT
和循环更容易。
:loop
if "%~1" NEQ "" (
call :argcheck
SHIFT
goto :loop
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.