Is this batch file correct?

Hamza Amin

Reputable
May 29, 2014
10
0
4,510
So I wrote a batch file t's purpose is to check the following:
1. is IP replying or not.
2. is destination reachable or not.
3. is connection timed out or not.
4. some other error.

@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 :eek:therError )
)
)

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 )


:eek:therError
echo Some Other Error >> Result.txt
GOTO End


:End
pause


Now i'm confused about the printing order of the things. I mean if the destination is unreachable then it's understood that connection is timed out and ip is not replying. Did I do this correctly and is there a easier way to do this because I think I made it a bit complex.

 
I don't think that you need to write the 0 1 0 to the results folder. You could make it more complex and write those to the IP folder to clean up the print in the Results folder. I don't see anything wrong with it and I don't see another way to make it less complex. I am definitely going to play around with it though to make it match my work environment!
 
The only thing that I see that is not required is the "Some Other Error" it is the same error as unreachable. There is another type of unreachable that can happen when you get a response from your own system. You could add that in there, but you will have to add a few more ifs and blahs.. I would just remove the "Some Other Error" as it really isn't needed.