IF EXIST with wildcards

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sfgman63
Posts: 8
Joined: 26 Nov 2009 01:32

IF EXIST with wildcards

#1 Post by sfgman63 » 26 Nov 2009 01:47

I'm trying to create a batch file using IF EXIST that searches for a specified file. The sub directory name changes everyday. If the sub directory name is hard coded then it works. I tried using a wild card for the sub directory and it doesn't work. Is it possible to use wild cards in this situation?

The temp2 directories are named like this:
temp2_11-20-09
temp2_11-21-09
temp2_11-22-09
temp2_11-23-09
temp2_11-24-09

@echo off
IF EXIST c:\temp\temp2*\test.txt (
echo "File exists"
) ELSE (
echo "file does not exist"
)

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 26 Nov 2009 11:10

Don't think that'll work -- I'd use a for loop:

Code: Select all

@echo off
cd /d c:\temp
for /d %%a in (temp2*) do (
   if exist "%%a\test.txt" echo File exists.&&goto continue
)
echo File does not exist.
:continue
echo Rest of your code here.

sfgman63
Posts: 8
Joined: 26 Nov 2009 01:32

#3 Post by sfgman63 » 26 Nov 2009 12:43

Thanks Larry...

I was wondering, the file I'm searching for is on a network drive so instead of using cd to change to the directory can I do something like this:

@echo off

for /d %%a in (temp2*) do (
IF EXIST "\\10.10.10.20\marketing\logs\%%a\test.txt" echo "File exist" && goto continue
)
echo File does not exist
:continue
echo Rest of your code here

Post Reply