通过类似开关的参数进行循环的批处理文件?

发布于 2024-10-21 04:56:17 字数 721 浏览 7 评论 0原文

我正在尝试循环传递给批处理文件的参数。根据参数,我想设置一个变量标志 true 或 false 以便稍后在脚本中使用

所以我的命令是“myscript.bat /u /p /s”

我的代码是:

FOR /f %%a IN ("%*") DO (
  IF /I "%%a"=="/u" SET UPDATE=Y
  IF /I "%%a"=="/p" SET PRIMARY=Y
  IF /I "%%a"=="/s" SET SECONDARY=Y
)

它仅在我有一个参数时才有效,它告诉我它将整个参数列表作为单个参数获取。我尝试过“delims=”但无济于事。关于获得每个间隔参数有什么想法吗?


向其中一个参数添加一个值怎么样?

myscript.bat /u /p /d TEST /s

:loop
IF "%~1"=="" GOTO cont
IF /I "%~1"=="/u" SET UPDATE=Y
IF /I "%~1"=="/p" SET PRIMARY=Y
IF /I "%~1"=="/s" SET SECONDARY=Y
IF /I "%~1"=="/d" SHIFT & SET DISTRO="%~1"
SHIFT & GOTO loop

:cont

但是与最后一个 IF 内联的 SHIFT 实际上并没有改变任何内容。 DISTRO 最终是“/d”而不是“TEST”

I'm trying to loop through the arguments that I am passing to a batch file. Based on the argument, I want to set a variable flag true or false for use later in the script

So my command is "myscript.bat /u /p /s"

And my code is:

FOR /f %%a IN ("%*") DO (
  IF /I "%%a"=="/u" SET UPDATE=Y
  IF /I "%%a"=="/p" SET PRIMARY=Y
  IF /I "%%a"=="/s" SET SECONDARY=Y
)

It only works if i have a single argument, which tells me that it is getting the entire list of arguments as a single argument. I've tried "delims= " but to no avail. Any thoughts on getting each spaced argument?


What about adding a value to one of the params?

myscript.bat /u /p /d TEST /s

:loop
IF "%~1"=="" GOTO cont
IF /I "%~1"=="/u" SET UPDATE=Y
IF /I "%~1"=="/p" SET PRIMARY=Y
IF /I "%~1"=="/s" SET SECONDARY=Y
IF /I "%~1"=="/d" SHIFT & SET DISTRO="%~1"
SHIFT & GOTO loop

:cont

But the SHIFT that comes inline with the last IF doesn't actually shift anything. DISTRO ends up being "/d" instead of "TEST"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南烟 2024-10-28 04:56:17

你离你原来的作品并不太远,而且因为我不喜欢 GOTO 循环,所以我想我应该发布这个:

FOR %%a IN (%*) DO (
  IF /I "%%a"=="/u" SET UPDATE=Y
  IF /I "%%a"=="/p" SET PRIMARY=Y
  IF /I "%%a"=="/s" SET SECONDARY=Y
)

它只使用一个参数的原因是过度使用引号。通过将 %* 放在引号中,您可以将整个命令行变成一个标记。此外,FOR 的 /F 变体也不是您想要的。文档可从 FOR /?应该有助于解决问题。

You're not too far off on your original piece, and since I dislike GOTO loops, I thought I'd post this:

FOR %%a IN (%*) DO (
  IF /I "%%a"=="/u" SET UPDATE=Y
  IF /I "%%a"=="/p" SET PRIMARY=Y
  IF /I "%%a"=="/s" SET SECONDARY=Y
)

The reason it was only working with one parameter is the over-use of quotes. By putting %* in quotes you were making the entire commandline one single token. also, the /F variant of FOR isn't what you were looking for either. The documentation available from FOR /? should help clear things up.

挽你眉间 2024-10-28 04:56:17

您可以使用 SHIFT、GOTO 和额外的 IF 循环遍历参数,以检查是否没有更多参数需要解析:

:loop
IF "%~1"=="" GOTO cont
IF /I "%~1"=="/u" SET UPDATE=Y
IF /I "%~1"=="/p" SET PRIMARY=Y
IF /I "%~1"=="/s" SET SECONDARY=Y
SHIFT & GOTO loop

:cont
...

有自己的参数时的情况)

UPDATE(解决参数 检查 /d 的 IF 语句确实有效。问题是整行会立即计算,并且 %~1 的两个实例都被替换为相同的值,此时为 /d

因此,基本上这种情况下的解决方案是使解释器与 IF /I "%~1"==" 分开评估 SET DISTRO="%~1" 部分/d”。对此可以有多种方法。例如,您可以简单地移动 SHIFT & SET DISTRO="%~1" 到下一行,如果 %~1 不是 /d

...
IF /I NOT "%~1"=="/d" GOTO skip_d
SHIFT & SET "DISTRO=%~1"
:skip_d
...

则 跳过它:方法可以是为DISTRO 分配一个特殊值(例如?),并在遇到/d 时进行切换。然后,在下一行中检查 DISTRO 是否具有该特殊值并将其设置为 %~1

...
IF /I "%~1"=="/d" SHIFT & SET DISTRO=?
IF "%DISTRO%"=="?" SET "DISTRO=%~1"
...

You could loop over the arguments using SHIFT, GOTO and an extra IF to check if there are no more parameters to parse:

:loop
IF "%~1"=="" GOTO cont
IF /I "%~1"=="/u" SET UPDATE=Y
IF /I "%~1"=="/p" SET PRIMARY=Y
IF /I "%~1"=="/s" SET SECONDARY=Y
SHIFT & GOTO loop

:cont
...

UPDATE (addressing the case when a parameter has an argument of its own)

The SHIFT in the IF statement that checks for /d does work. The issue is that the entire line is evaluated at once and both instances of %~1 get replaced with the same value, which is /d at that point.

So, basically the solution in this case would be to cause the interpreter to evaluate the SET DISTRO="%~1" part separately from the IF /I "%~1"=="/d". There can be various approaches to this. For instance, you could simply move SHIFT & SET DISTRO="%~1" to the next line and skip it if %~1 is not /d:

...
IF /I NOT "%~1"=="/d" GOTO skip_d
SHIFT & SET "DISTRO=%~1"
:skip_d
...

Another method could be to assign a special value (e.g. a ?) to DISTRO and shift when /d is encountered. Then, on the next line, check if DISTRO has that special value and set it to %~1:

...
IF /I "%~1"=="/d" SHIFT & SET DISTRO=?
IF "%DISTRO%"=="?" SET "DISTRO=%~1"
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文