Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 652 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does the command "if %errorlevel% 1 do" result in an exit of batch file processing?

#1
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:

[To see links please register here]

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!**
Reply

#2
Try either `IF NOT ERRORLEVEL 0` or `IF NOT %ERRORLEVEL%==0`
Reply

#3
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:

+ [What are the ERRORLEVEL values set by internal cmd.exe commands?](

[To see links please register here]

)
+ [Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?](

[To see links please register here]

)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through