Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: ERRORLEVEL = 0 within FOR loop  (Read 20806 times)

0 Members and 1 Guest are viewing this topic.

ShadyDave

    Topic Starter


    Rookie

    ERRORLEVEL = 0 within FOR loop
    « on: April 28, 2010, 07:25:36 AM »
    I have posted previously about my DR check batch file, but just one issue remains...

    I have this code within the batch...

    example.txt contains the following line for testing...
    Quote
    \autoexec.bat

    The for loop contains; - I changed the FIND to wwwwwwww to force a failure.
    Code: [Select]
    set TIB=example.txt
    set SERVER=C:
    for /f "eol=; tokens=* delims=" %%i in (%TIB%) do (
        dir "%SERVER%%%i" | find "wwwwwwww" >nul
        if %ERRORLEVEL%==0 (
            echo Located - %SERVER%%%i >> %logfile%
        ) else (
            echo ***ERROR*** - %SERVER%%%i, does not exist or incorrect file date >> %logfile%
        )
    )

    For some reason ERRORLEVEL always returns zero even if the file doesn't exist, I'm assuming that's because the FOR command didn't error even though the dir command did.
    If I run the DIR command outside the FOR loop it returns errorlevel 1

    Does anybody know of a way around this?

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: ERRORLEVEL = 0 within FOR loop
    « Reply #1 on: April 28, 2010, 07:37:01 AM »
    you can try

    Code: [Select]
    set TIB=example.txt
    set SERVER=C:
    for /f "eol=; tokens=* delims=" %%i in (%TIB%) do (
        dir "%SERVER%%%i" | find "wwwwwwww" >nul
        if ERRORLEVEL 1 (
            echo ***ERROR*** - %SERVER%%%i, does not exist or incorrect file date >> %logfile%
        ) else (
            echo Located - %SERVER%%%i >> %logfile%

        )
    )

    Or:
    setlocal enabledelayedexpansion
    set TIB=example.txt
    set SERVER=C:
    for /f "eol=; tokens=* delims=" %%i in (%TIB%) do (
        dir "%SERVER%%%i" | find "wwwwwwww" >nul
        if %ERRORLEVEL%==0 (
            echo Located - %SERVER%%%i >> %logfile%
        ) else (
            echo ***ERROR*** - %SERVER%%%i, does not exist or incorrect file date >> %logfile%
        )
    )
    endlocal
    I was trying to dereference Null Pointers before it was cool.

    Salmon Trout

    • Guest
    Re: ERRORLEVEL = 0 within FOR loop
    « Reply #2 on: April 28, 2010, 08:02:36 AM »
    try this

    variables set and read inside parenthetical expressions need delayed expansion.

    setlocal enabledelayedexpansion
    set TIB=example.txt
    set SERVER=C:
    for /f "eol=; tokens=* delims=" %%i in (%TIB%) do (
        dir "%SERVER%%%i" | find "wwwwwwww" >nul
        if !ERRORLEVEL! EQU 0 (
            echo Located - %SERVER%%%i >> %logfile%
        ) else (
            echo ***ERROR*** - %SERVER%%%i, does not exist or incorrect file date >> %logfile%
        )
    )

    ShadyDave

      Topic Starter


      Rookie

      Re: ERRORLEVEL = 0 within FOR loop
      « Reply #3 on: April 29, 2010, 07:39:36 AM »
      Thanks, not quite correct but you gave me a good pointer...

      I just needed to replace % with !...
      Code: [Select]
      if !ERRORLEVEL!==0
      setlocal command was already added but not included it in my post above  ;)

      I've noticed another issue, but I'll create a new post  ;D

      Salmon Trout

      • Guest
      Re: ERRORLEVEL = 0 within FOR loop
      « Reply #4 on: April 29, 2010, 09:56:51 AM »
      not quite correct

      Code: [Select]
      if !ERRORLEVEL!==0
      Code: [Select]
      if !ERRORLEVEL! EQU 0
      These two are equivalent; I don't understand why you call my version "not quite correct".

      ShadyDave

        Topic Starter


        Rookie

        Re: ERRORLEVEL = 0 within FOR loop
        « Reply #5 on: April 29, 2010, 04:12:13 PM »
        It created an error using the EQU for some reason :-(

        Helpmeh



          Guru

        • Roar.
        • Thanked: 123
          • Yes
          • Yes
        • Computer: Specs
        • Experience: Familiar
        • OS: Windows 8
        Re: ERRORLEVEL = 0 within FOR loop
        « Reply #6 on: April 29, 2010, 06:11:31 PM »
        It created an error using the EQU for some reason :-(
        What was the line?
        If !ERRORLEVEL! EQU 0 echo There was no error...

        should work properly.
        Where's MagicSpeed?
        Quote from: 'matt'
        He's playing a game called IRL. Great graphics, *censored* gameplay.

        Salmon Trout

        • Guest
        Re: ERRORLEVEL = 0 within FOR loop
        « Reply #7 on: April 30, 2010, 12:46:31 AM »
        What was the line?
        If !ERRORLEVEL! EQU 0 echo There was no error...

        should work properly.

        It works properly for me.