Nic3500
Nic3500

Reputation: 8611

If exist with multiple wildcards

These are the requirements:

What I have tried (applies to only 1 wildcard):

SET wildcard=a*.txt
cd c:\somedirectory
IF EXIST "%wildcard%" (
    REM PROCESS THE FILES
) ELSE (
    ECHO There are no files that match that wildcard, please review.
)

This obviously only works if the wildcard is unique.

What I have tried:

SET wildcard=a*.txt b*.txt
cd c:\somedirectory
FOR %%A IN (%wildcard%) DO (
    ECHO %%A
)

This prints the files that match a*.txt or b*.txt. I do not want that expansion to occur. I need the actual wildcard value to go inside the loop.

With expansion, I cannot tell the user that some his wildcards do not have files. E.g. there are files a*.txt, but no files b*.txt. I need to tell that to the user. Something like:

a*.txt: there are files.
b*.txt: there are no files, please review.

Something like (this does not work, just the idea of what I am looking for):

SET wildcard=a*.txt b*.txt c*.txt
cd c:\somedirectory

REM loop on the wildcards
FOR %%A IN (%wildcard%) DO (

    REM verify if there are files for that wildcard
    IF EXIST %%A (

        REM loop on the files from the specific wildcard
        FOR %%F IN (%%A) DO (
            REM PROCESS THE FILES
        )
    ) ELSE (
        ECHO This pattern %%A has no files associated
    )
)

Basically, can I prevent the expansion of values in %wildcard% inside a IF statement?


For @double-beep's comment:

But how do I do that flexible, based on what the user puts in the wilcard variable? I thought of looping on the values of wildcard, but FOR does the expansion, which I do not want.

Upvotes: 2

Views: 2584

Answers (1)

aschipfl
aschipfl

Reputation: 34909

What about this piece of code, using call with parameters, which do not resolve wildcards:

@echo off
set "WildCard=a*.txt b*.txt *.vi"

call :LOOP %WildCard%
rem ...
goto :EOF


:LOOP
    if "%~1"=="" goto :EOF
    if exist "%~1" echo There are files that match the pattern: "%~1"
    shift /0
    goto :LOOP

Upvotes: 1

Related Questions