问题描述
我的代码几乎可以自我解释:
My code is pretty much self explanatory :
@echo off
set /a n=0
:doitagain
set /a n+=1
echo Pinging : 10.0.0.%n%
ping 10.0.0.%n%
if %n% lss 255 goto doitagain
pause >nul
但是它总是ping 10.0.0.1,所以我想这里需要细微调整...
but it always pinging 10.0.0.1 so I guess tiny tweak needed here...
推荐答案
正如npocmaka在他的评论中所述,当您使用在循环内修改的变量时,您总是需要延迟扩展.第一步是在代码的开头添加SETLOCAL EnableDelayedExpansion
位置.第二步是访问用!varname!
而不是%varname%
在循环内部修改的变量.在您的情况下,将是这样的:
As npocmaka stated in his comment you always need delayed expansion when you are working with variables which are modified inside a loop. The first step is to add SETLOCAL EnableDelayedExpansion
somwhere at teh beginning of your code. The second step is to access the variables which are modified inside the loop with !varname!
instead of %varname%
. In your case it would be something like this:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
...
set /a n=!n!+1
echo Pinging : 10.0.0.!n!
ping 10.0.0.!n!
if !n! lss 255 goto doitagain
...
这篇关于批处理文件::如果LSS无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!