2

I have a lot of internet connection downtimes recently. So I'd like to make a list of each time I had issues to send it to my ISP. In order to make it a bit less tedious, I figured out, it would be nice to make a kind of report and send it in as a log file. I stumbled upon a few solutions, but the best I found was on this site: Ping with timestamp by Joost Kuin.

I have adjusted this code a bit, but I ran into one issue. If I try using if not %errorlevel% 0 do (...), if %errorlevel% 1 do (...) and try to run .bat file, Windows command processor window just pops up and disappears. Without this condition the batch file runs just fine. But it does not do what it is made for. I want to make 2 files, one with plain ping log, the other one only with timeouts and destination host not reachable timestamps. It's my first time scripting for Windows. I am used to code in C++ back in the days. Instead of host_ip I got IP address of first connection to the outside network.

@echo off

set host=host_ip 
set logfile=Ping_test.log
set logfile_fail=Ping_test_fail.log

echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1') do (echo (%%A>>%logfile% && GOTO Ping)

:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    if not %errolevel% 0 do (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% errorlvel %errorlevel% %%A>>%logfile_fail%) 
    echo %errorlevel% %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL 
    GOTO Ping
)

EDIT:

Thank you for helpful advices. I have tried to implement them as well as continue searching for solution on my own. And after a lot of time and frustration it is done. I had to change a few lines (included them below) according to how for /F command instructions are executed (in another command process). But in the end I got my arguably first actually useful bit of code done and to be honest it made me realize how useful batch scripting is. ;)

:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ^& call echo %%^^errorlevel%%^>error.level') do (
    set /p elv=<"error.level" 
    if not !elv!==0 (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile_fail%)
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    echo elv %elv% %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL 
    GOTO Ping
)

So once again

Huge thanks for helping me out!

Community
  • 1
  • 1
Marcin
  • 79
  • 1
  • 1
  • 7
  • If you want to test this [Pinging Multiple PCs and Adding Text](http://stackoverflow.com/questions/38636170/pinging-multiple-pcs-and-adding-text?answertab=active#tab-top) or this one on pastebin [Multi Ping Tester with colors](http://pastebin.com/zjYwSqUM) – Hackoo Sep 11 '16 at 11:47
  • You have a spelling mistake, `if not %errolevel%` should be `if not %errorlevel%` – DavidPostill Sep 11 '16 at 20:43
  • @DavidPostill thanks for spotting it, i was working on the code a bit and this part is basically gone now though - i replaced with a variable – Marcin Sep 11 '16 at 23:12

2 Answers2

7

Open a command prompt window, run there if /? or help if and read all pages of output help for command IF.

The syntax is:

if errorlevel 1 echo Previous command most likely exited with an error code.

This means if exit code of previous command or application is greater or equal 1 then output the message that previous command most likely exited with an error code greater 0.

if not errorlevel 1 echo Previous command most likely finished successfully.

This means if exit code of previous command or application is lower than 1 (Not Greater Or Equal 1) then output the message that previous command most likely finished successfully with exit code 0. Negative exit codes are usually not used and Microsoft strongly recommends not using negative exit codes.

It is also possible to reference the string value of the environment variable ERRORLEVEL by using with immediate expansion %ERRORLEVEL% or with delayed expansion !ERRORLEVEL! whereby delayed expansion is needed instead of immediate expansion in case of the errorlevel variable reference is within a command block. A command block is everything from an opening parenthesis to matching closing parenthesis.

But not using if errorlevel X or if not errorlevel X requires an operator like == between environment variable reference and the value, e.g.

if %ERRORLEVEL% == 1 echo Exit code of previous command/application is 1.
if %ERRORLEVEL% == 0 echo Exit code of previous command/application is 0.

And take a look at:

Mofi
  • 38,783
  • 14
  • 62
  • 115
2

Try either IF NOT ERRORLEVEL 0 or IF NOT %ERRORLEVEL%==0

Compo
  • 30,301
  • 4
  • 20
  • 32