jeudi 19 décembre 2019

How do I ignore the "File Not Found" string in a batch file?

I'm writing a batch file that will work with sub folders in a Repository folder.

Example:

Repository\Landscape
Repository\Batman

Any file under the Repository folder that isn't a folder, is ignored. To work with the subfolder, I use a for /f loop with the dir command, but if the folder is empty, it returns File Not Found. Is there a way I can filter that output out? I tried string comparison, but it doesn't seem to work.

Code:

setlocal EnableDelayedExpansion

set repository=%userprofile%\Repository

if exist "%repository%" (
    cd "%repository%"
    for /f %%G in ('dir /l /b /a:d %repository%') DO (
        rem We know we're only working with folders.
        set subfolder=%%G
        echo !subfolder!
        for /f %%n in ('dir /b /a:-d !subfolder!') DO (
            set "filename=%%n"
            set "ignorestring=File Not Found"
            if /I NOT !filename!==!ignorestring! (
                echo %%n
            )
        )
    ) 
)

Aucun commentaire:

Enregistrer un commentaire