0

I am trying to use !errorlevel! as a condition inside a loop. I pass error level in a call and then I try and use findstr again to set errorlevel but the condition is not seeing the change.

   setlocal EnableDelayedExpansion
   for /F %%i in  (%Newzips.lst%) do    (
    echo unzipping %%i >> test.log  
    wzunzip -o %%i
    call :startloop 
    )
   exit /B

   :startloop

   dir /b *.dat > %Stagedat.lst%
     for /F %%l in (%Stagedat.lst%) do (        
        dir /b E:\NOMADD_FILES\AV\*.dat > %AVdat.lst%
        findstr /i /c %%l %AVdat.lst%
        echo top error level - !errorlevel! 
        call :checkdup %%l !errorlevel!          
    )   
    exit /B

   :checkdup    
    if !errorlevel! == 0 (
        dir /b E:\NOMADD_FILES\AV\*.dat > %AVdat.lst%
        findstr /i %1 %AVdat.lst% >> test.log
        echo  found match so sleep e >> test.log
        echo error level - !errorlevel! >> test.log
        sleep 3
        echo duplicate in  AVdat.lst >> test.log
        call :checkdup %1 !errorlevel!
)
    if !errorlevel! == 1 (
        echo copying file %2 >> test.log
        move /Y %1 E:\NOMADD_FILES\AV
       sleep 5
       dir /b E:\NOMADD_FILES\AV\*.dat > %AVdat.lst%
       echo moveing AVdat.lst >> test.log
       echo error level - !errorlevel! >> test.log
       type %AVdat.lst% >> test.log
)
    exit /B



    :end 

So what is happening is after I get down to :checkdup and findstr runs again, !errorlevel! is being updated to 1 but it is still staying inside the top if statement.

This is a sample from the log.

    top error level - 0 
    bcs3_ammo_inv_updates.dat
    found match so sleep e 
    error level - 0 
    duplicate in  AVdat.lst 
    bcs3_ammo_inv_updates.dat
    found match so sleep e 
    error level - 0 
    duplicate in  AVdat.lst 
    bcs3_ammo_inv_updates.dat
    found match so sleep e 
    error level - 0 
    duplicate in  AVdat.lst 
    found match so sleep e 
    error level - 1 
    duplicate in  AVdat.lst 
    found match so sleep e 
    error level - 1 
    duplicate in  AVdat.lst 
    found match so sleep e 
    error level - 1 

So error level is being set to 1 but it is stuck in the if !errorlevel! == 0 condition. It should go to the other check and continue through the for loop. I've been stuck on this for a couple days now trying many different combinations to get it to work.

Thanks

3
  • is this recursion in first if statement intentional ? Commented Oct 28, 2013 at 15:17
  • 1
    you should better use a GUI. Or you start to learn something about Windows Command Shell Language (aka Batch syntax). Commented Oct 28, 2013 at 15:19
  • Yes the recurion is intentional. If there is a duplicate file it needs to wait and then check again. Once there isn't a duplicate it should move the file over. Once the error level changes to 1 it should move to the lower condition but it keeps going to the top condition. Commented Oct 28, 2013 at 15:26

1 Answer 1

2
   :checkdup    

    if !errorlevel! == 0 (
        ...
        echo error level - !errorlevel! >> test.log
        sleep 3

        rem AND AFTER SLEEP, which is the value of errorlevel?

        call :checkdup %1 !errorlevel!
    )
Sign up to request clarification or add additional context in comments.

4 Comments

You see that the error level is changed to 1 in the log above but it still stays in the if !errorlevel! == 0 condition. What I am trying to do is not overwrite duplicate files between a stage area and process area. Once the files process out I move another one over.
NO. Sleep command resets the errorlevel to 0. It is not an usual variable. It is updated after each command execution. The batch executes sleep.exe, it gives no error so errorlevel is 0
Wow, thank you so much. I can't believe something that small was the issue. I moved sleep 3 to the first command under the if statement and it worked perfect.
Clarification - External commands always set ERRORLEVEL, regardless if success or failure. Internal commands in .BAT files only set errorlevel upon error - success does not reset ERROLEVEL to 0. But internal commands in .CMD files always set ERRORLEVEL, even if success.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.