问题
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
but it always pinging 10.0.0.1 so I guess tiny tweak needed here...
回答1:
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
...
来源:https://stackoverflow.com/questions/33366300/batch-file-if-lss-is-not-working-properly