Windows batch-file errorlevel question
Asked Answered
I

2

0

I've got a batch file that parses a bunch of file names out of a text file and concatenates them into a single strong - it was previously discussed here. However, I don't want the string to contain a file if the file throw an error when I run it through some command (like a VCS check, for example). Here's my attempt:

set FILE_LIST=
for /f %%x in (temp.txt) do (

:: perform a VCS test
accurev diff -b %%x

:: skip concatenation if error level is > 2
IF errorlevel 2 GOTO NEXT

:: perform the concatenation
set FILE_LIST=!FILE_LIST! %%x

:NEXT
:: print a message if here due to an error above
IF errorlevel 2 echo VCS problem with this file: %%x
)

The problem is - the script appears to stop executing the entire for-loop as soon as it finds one errorlevel greater than 2. If there are five files in the list and the third one has a VCS problem - the script only handles the first two.

Insurable answered 1/10, 2009 at 21:35 Comment(3)
It looks like the errorlevel is not reset on each iteration. Is it possible to manually reset it to 0 at the end of the loop?Outlook
mmyers: it would seem that the advice from the internets is to never manually set the errorlevel variable.Insurable
ahh - I see hyperlink formatting does not work in comments.Insurable
A
0
setlocal ENABLEDELAYEDEXPANSION
set FILE_LIST=
for /f %%x in (temp.txt) do (
    accurev diff -b "%%~x"
    IF errorlevel 2 (
        echo VCS problem with this file: %%~x
    ) ELSE (
        set FILE_LIST=!FILE_LIST! %%x
    )
)
Advocation answered 1/10, 2009 at 21:50 Comment(0)
A
0

IF ERRORLEVEL construction has one strange feature... it returns TRUE if the return code was equal to or higher than the specified errorlevel.

Reference this link to understand how to deal with that "feature".

Aluminiferous answered 1/10, 2009 at 21:38 Comment(1)
that's what I'm after here - for this executable (accurev) the only "good" error levels are 0 or 1, so anything above 2 should be caught. I'm confused as to why it appears to bail out of the for-loop when it catches an error.Insurable
A
0
setlocal ENABLEDELAYEDEXPANSION
set FILE_LIST=
for /f %%x in (temp.txt) do (
    accurev diff -b "%%~x"
    IF errorlevel 2 (
        echo VCS problem with this file: %%~x
    ) ELSE (
        set FILE_LIST=!FILE_LIST! %%x
    )
)
Advocation answered 1/10, 2009 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.