title: Why does the command "if %errorlevel% 1 do" result in an exit of batch file processing?
body: <p>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: <a href="https://stackoverflow.com/questions/24906268/ping-with-timestamp">Ping with timestamp</a> by Joost Kuin.</p>
  
  <p>I have adjusted this code a bit, but I ran into one issue. If I try using <code>if not %errorlevel% 0 do (...)</code>, <code>if %errorlevel% 1 do (...)</code> 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 <em>destination host not reachable</em> 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.</p>
  
  <pre><code>@echo off
  
  set host=host_ip 
  set logfile=Ping_test.log
  set logfile_fail=Ping_test_fail.log
  
  echo Target Host = %host% &gt;%logfile%
  for /f "tokens=*" %%A in ('ping %host% -n 1') do (echo (%%A&gt;&gt;%logfile% &amp;&amp; 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&gt;&gt;%logfile%
      if not %errolevel% 0 do (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% errorlvel %errorlevel% %%A&gt;&gt;%logfile_fail%) 
      echo %errorlevel% %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
      timeout 1 &gt;NUL 
      GOTO Ping
  )
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>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 <code>for /F</code> 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. ;)</p>
  
  <pre><code>:Ping
  for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ^&amp; call echo %%^^errorlevel%%^&gt;error.level') do (
      set /p elv=&lt;"error.level" 
      if not !elv!==0 (echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A&gt;&gt;%logfile_fail%)
      echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A&gt;&gt;%logfile%
      echo elv %elv% %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
      timeout 1 &gt;NUL 
      GOTO Ping
  )
  </code></pre>
  
  <p>So once again</p>
  
  <p><strong>Huge thanks for helping me out!</strong></p>