Search code examples
batch-filefindstrerrorlevel

List all .zip files that contain an error file (batch, cmd)


I have a folder with a bunch of zip files, some of which contain an "*error.pdf" file. I need to make an "errors.txt" file containing the filenames of the zip files that contain such an error file.

I need help with the condition. FINDSTR sets ERRORLEVEL to 0 if the string is found, to 1 if it is not. I've tried if not errorlevel 1 and if %ERRORLEVEL% == 0.

for %%X in (*) do (
    "c:\Program Files\7-Zip\7z.exe" l "%%X" | findstr error > nul
    if errorlevel 0(
        ECHO "%%X" >> errors.txt
    )
)

Right now nothing happens (if condition is always false), if I set the if condition to always be true it writes all filenames to errors.txt.

"c:\Program Files\7-Zip\7z.exe" l "%%X" | findstr error does output the filenames of the *error.pdfs


Solution

  • The problem was a missing space between if errorlevel 0 and ( (thanks to aschipfl).

    The correct condition for the if statement was also if not errorlevel 1 as mentioned by Magoo

    This is the working code (with improvements mentioned by aschipfl)

    for %%X in (*.zip) do (
        "c:\Program Files\7-Zip\7z.exe" l "%%X" | findstr error > nul
            if not errorlevel 1 (
            ECHO %%X >> errors.txt
            )
            )