Windows命令行 - 尝试在同一行中运行2个命令时错误
我有一个.bat文件正常工作:
set list=1 5 10 18 22 27
for %%p in (%list%) do @ping 192.168.0.%%p -w 10 -n 1 | findstr "Reply Request"
当我尝试在线运行它时,与& 或或&& 在这样的一行中结合使用
set list=1 5 10 18 22 27 && for %%p in (%list%) do @ping 192.168.0.%%p -w 10 -n 1 | findstr "Reply Request"
。说:
%%p was unexpected at this time.
我在做什么错?
如果组合命令在BAT文件中确实有效,但是如果粘贴在命令行中。
这样做的目的是让Excel生成一行复制命令,以在命令提示符中运行,因为创建批处理文件不允许在远程系统上。
解决: 谢谢Magoo和Stephan! 解决方案是包围数组:
set "list=1 5 10 18 22 27"
并使用%p,如果%% P:
for %p in (%list%) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"
最终结果:
set "list=1 5 10 18 22 27" && for %p in (%list%) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"
:)
ps: magoo建议更简化的解决方案:
for %p in (1 5 10 18 22 27) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"
简单,单个命令:)
I have a .bat file that works properly:
set list=1 5 10 18 22 27
for %%p in (%list%) do @ping 192.168.0.%%p -w 10 -n 1 | findstr "Reply Request"
When I try to run it inline, combined with & or && in one line like this
set list=1 5 10 18 22 27 && for %%p in (%list%) do @ping 192.168.0.%%p -w 10 -n 1 | findstr "Reply Request"
I get an error saying:
%%p was unexpected at this time.
What am I doing wrong?
If the combined command is in the bat file it does work, but not if pasted in the command line.
The purpose of this is to have excel generate one line copy-paste command to be run in the command prompt, as creation the batch file is not allowed on the remote system.
SOLVED:
Thank you Magoo and Stephan!
The solution is to enclose array:
set "list=1 5 10 18 22 27"
And to use %p instead if %%p:
for %p in (%list%) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"
So final result:
set "list=1 5 10 18 22 27" && for %p in (%list%) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"
does work :)
PS: Magoo suggested more streamlined solution:
for %p in (1 5 10 18 22 27) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"
Simple, single command :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
list
解析行时的值是不确定的,因此> batch替换 nother> nothing (
和执行的解决结果是list list
) list%如此,
使用
设置“ var = value”
用于设置字符串值 - 这避免了落后空间引起的问题。不要分配终端后斜线, space 或“
- 从元素构建路径名 - 违反直觉,它可能会使过程更容易。The value of
list
is undefined when the line is parsed, so batch substitutes nothing (the "contents" oflist
) for%list%
and the resolved result that is executed isSo, what's wrong with
Use
set "var=value"
for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal backslash, Space or"
- build pathnames from the elements - counterintuitively, it is likely to make the process easier.要使用在同一行(或代码块)中定义的变量,您需要延迟扩展
默认情况下禁用了延迟的扩展,因此您需要在启用延迟扩展的情况下启动
cmd
的新实例:(虽然我更喜欢Magoo的解决方案)
To use a variable defined in the same line (or code block), you need delayed expansion
Delayed expansion is disabled by default, so you need to start a new instance of
cmd
with delayed expansion enabled:(Although I'd prefer Magoo's solution)