is this batch file correct

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hamza4best
Posts: 1
Joined: 25 Jun 2014 13:27

is this batch file correct

#1 Post by hamza4best » 25 Jun 2014 13:34

I wrote a batch file that gives out the following result
1. If a particular IP address is replying or not.
2. If host is reachable or not.
3. If IP is timed out or not.
4. Some other error

Code: Select all

@echo off

set /p ip="Enter IP Address: "
ping %ip% -n 2 > nul > IP.txt

if %ERRORLEVEL% == 0 (
   echo Ping To %ip% Successful > Result.txt
   ) else echo Ping To %ip% Failed > Result.txt

findstr /m "unreachable" IP.txt
set ur=%ERRORLEVEL%

findstr /m "timed out" IP.txt
set t=%ERRORLEVEL%

findstr /m "Reply" IP.txt
set r=%ERRORLEVEL%

echo %ur% >> Result.txt
echo %r%  >> Result.txt
echo %t%  >> Result.txt

if %ur%==1 (
   if %r%==1 ( 
      if %t%==1 ( CALL :otherError ) 
   )
)

   if %ur% == 1 ( echo Reachable >> Result.txt

      if %t% == 1 ( echo Not Timed Out >> Result.txt
      

         if %r% == 0 ( echo System Is Replying >> Result.txt 

               CALL :End )  else ( echo Not Replying >> Result.txt
                         CALL :End )   
   
   ) else ( echo Timed Out >> Result.txt
                 CALL :End )   
) else ( echo Unreachable >> Result.txt
         CALL :End )   


:otherError
 echo Some Other Error >> Result.txt   
 GOTO End


:End
pause    

However I'm confused about the sequence of things. I mean if a host is unreachable then it's obvious that ip is not replying and it is timed out right?
Just take a look and see if it is correct or not.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: is this batch file correct

#2 Post by foxidrive » 26 Jun 2014 00:57

hamza4best wrote:I wrote a batch file that gives out the following result
1. If a particular IP address is replying or not.
2. If host is reachable or not.
3. If IP is timed out or not.
4. Some other error

However I'm confused about the sequence of things. I mean if a host is unreachable then it's obvious that ip is not replying and it is timed out right?
Just take a look and see if it is correct or not.


Yes, you arrange your logic so that for an unreachable address then quit or go to the next IP address.

Post Reply