errorlevel does not update

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

errorlevel does not update

Post by MigrationUser »

03 Feb 2009 17:10
Dmi3

I need to check if the app exited gracefully, but it seems findstr and errorlevel do not work the way I need it. Here is what I have so far:

Code: Select all

if exist trail.txt.* (
  FOR %%G IN (trail.txt.*) do (findstr /C:"!End of Trail File" "%%G")
    if %ERRORLEVEL% GTR 0 (
   echo Your previous session exited unexpectedly. 
    move %%G dbg_%%G
  ) else (
    del /f /q %%G 
  )
)
Also %%G does not have the value of the file name :wall:

Anybody have an example?

----------------------------

#2 03 Feb 2009 17:41
avery_larry


%errorlevel% works like any other standard variable. If you put it inside an if statement (or a for loop or other things), it's processed when the if command is called -- not inside the if (all variables inside the IF parentheses are evaluated when the IF is called -- not in the midst of the IF statement).

There are a variety of ways to fix this. Probably the best for what you're doing is to use the "if errorlevel" code instead of %errorlevel% like this:

Code: Select all

if exist trail.txt.* (
  FOR %%G IN (trail.txt.*) do (findstr /C:"!End of Trail File" "%%G")
    if errorlevel 1 (
   echo Your previous session exited unexpectedly. 
    move %%G dbg_%%G
  ) else (
    del /f /q %%G 
  )
)
IF ERRORLEVEL doesn't use variables, so it evaluates the errorlevel at that specific time.

Another possibility is to use delayedexpansion -- which means it waits as long as possible before evaluating a variable -- so instead of evaluating %errorlevel% when the if statement is called, it evaluates it when the nested if statement is called:

Code: Select all

setlocal enabledelayedexpansion
if exist trail.txt.* (
  FOR %%G IN (trail.txt.*) do (findstr /C:"!End of Trail File" "%%G")
    if !errorlevel! GTR 0 (
   echo Your previous session exited unexpectedly. 
    move %%G dbg_%%G
  ) else (
    del /f /q %%G 
  )
)
Note that I don't know what that would do to the "!" that's part of your findstr statement (delayedexpansion uses ! for delayed variables so the ! becomes a special character of sorts).

The final way to accomplish this is by calling a subroutine instead of keeping it all inside the if statement:

Code: Select all

if exist trail.txt.* call :subroutine
<other code here>
goto :eof

:subroutine
FOR %%G IN (trail.txt.*) do (findstr /C:"!End of Trail File" "%%G")
if %ERRORLEVEL% GTR 0 (
   echo Your previous session exited unexpectedly. 
   move %%G dbg_%%G
   ) else (
      del /f /q %%G 
)
goto:eof
As far as %%G -- it'll only be defined inside the for loop -- which means you would have to pull the rest of the code inside the for loop like this (based on your original code):

Code: Select all

if exist trail.txt.* (
   FOR %%G IN (trail.txt.*) do (
      findstr /C:"!End of Trail File" "%%G"
      if %ERRORLEVEL% GTR 0 (
         echo Your previous session exited unexpectedly. 
         move %%G dbg_%%G
         ) else (
            del /f /q %%G 
      )
   )
)
But then you're back to the %errorlevel% problem. Combining the 2 concepts above I would come up with something like this:

Code: Select all

if exist trail.txt.* (
   FOR %%G IN (trail.txt.*) do (
      findstr /C:"!End of Trail File" "%%G"
      if errorlevel 1 (
         echo Your previous session exited unexpectedly. 
         move %%G dbg_%%G
         ) else (
            del /f /q %%G 
      )
   )
)
Finally, note that the "IF ERRORLEVEL" means (from the if /? help prompt) "Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified."

So essentially IF ERRORLEVEL 1 is the same as IF %ERRORLEVEL% GEQ 1 only not restricted to obeying the rules of variables (when they're evaluated).

----------------------------

#3 05 Feb 2009 17:47
Dmi3


Thank you avery_larry!
Post Reply