在循环中设置变量时遇到问题

时间:2013-10-28 14:59:49

标签: variables loops batch-file errorlevel

我正在尝试使用!errorlevel!作为循环内的条件。我在调用中传递错误级别,然后我再次尝试使用findstr来设置errorlevel,但条件没有看到更改。

   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 

所以发生的事情是在我开始之后:checkdup和findstr再次运行,!errorlevel!正在更新为1,但它仍然保持在顶部if语句中。

这是日志中的一个示例。

    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 

因此错误级别被设置为1,但它被卡在if!errorlevel中! == 0条件。它应该转到另一个检查并继续通过for循环。我已经坚持了几天,现在尝试了许多不同的组合来让它发挥作用。

谢谢

1 个答案:

答案 0 :(得分: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!
    )