0

Can anybody suggest what is wrong in following code.

@echo off

set /a x=1

:while
set /a sta=0

if %x% lss 5 (

    for /f "tokens=4" %%i in ('ping 127.0.0.%x%') do (set sta="1"
        if "%%i" == "bytes=32" (echo 111111111
            set /a sta=1
        )
    )


    if %sta% == 1 echo 127.0.0.%x% - ONLINE
    if %sta% == 0 echo 127.0.0.%x% - OFFLINE

    set /a x+=1
    goto while
)

pause

It is always going to if condition 'if %sta% == 0'. It is never going to condition if %sta% == 1 .

It seems the sta variable value is not changing.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Matt
  • 1,953
  • 1
  • 19
  • 42
  • Although this may not be considered an answer, I would recommend using Powershell to do this kind of extensive conditions and looping. There is much better tool support for Powershell (Powershell ISE etc.) and debugging this kind of issue will be a snap. It does not require much effort to convert this kind of logic in Powershell too. – murtazat Apr 28 '17 at 04:45
  • 2
    you need [delayed expansion](https://ss64.com/nt/delayedexpansion.html) – npocmaka Apr 28 '17 at 04:59
  • @npocmaka thanks it worked – Matt Apr 28 '17 at 05:10

1 Answers1

1

Thanks @npocmaka for valuable information. Following code works,

@echo off

SETLOCAL EnableDelayedExpansion

set /a x=1

:while
set sta=0

if %x% lss 5 (

    for /f "tokens=4" %%i in ('ping 127.0.0.%x%') do (
        if "%%i" == "bytes=32" (
            set sta=1
        )
    )

    if !sta! == 1 echo 127.0.0.%x% - ONLINE
    if !sta! == 0 echo 127.0.0.%x% - OFFLINE

    set /a x+=1
    goto while
)

pause
Matt
  • 1,953
  • 1
  • 19
  • 42