Page 1 of 1

IF EXIST with wildcards

Posted: 26 Nov 2009 01:47
by sfgman63
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"
)

Posted: 26 Nov 2009 11:10
by avery_larry
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.

Posted: 26 Nov 2009 12:43
by sfgman63
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