tags:

views:

42

answers:

2

I'm currently trying to search directories for any file labelled "??.??.????.xls" (for mm.dd.yyyy.xls). The problem I have is that the code I'm using also matches filenames such as "my-restaurant.12.01.2006.xls". I only want to match filenames with specifically the notation I used above.

Dim Invoices As FileSearch
Set Invoices = Application.FileSearch

With Invoices
    .Filename = "??.??.????.xls"
    ' invDir is a directory I chose earlier on
    .LookIn = invDir
    .SearchSubFolders = True
    .MatchTextExactly = True
End With

Is there something I'm missing? I know I could do yet another check in my code elsewhere to make sure the filename's length is 14 characters, but is there a parameter I'm not considering in the FileSearch?

A: 

Try using the NewSearch method like so:

Dim Invoices As FileSearch
Set Invoices = Application.FileSearch

With Invoices
    .NewSearch
    .Filename = "??.??.????.xls"
    '' invDir is a directory I chose earlier on
    .LookIn = invDir
    .SearchSubFolders = True
    .MatchTextExactly = True
End With

The remarks in the documentation lead me to believe that the search criteria has not been set/reset:

Search criteria settings are retained throughout an application session. Use this method every time you change search criteria. This method will not reset the value of the LookIn property.

Buggabill
That sadly doesn't do anything for his problem
jitter
A: 

After some research and toying around I conclude that an exact match without lenght check can't be done.

Btw. the Application.FileSearch object isn't longer available from Office 2007+ so maybe consider using the FileSystemObject instead

jitter
I should have mentioned that I'm using Office 2003. Thanks though :)
JakeTheSnake
In the end, I just put a Len() check on the file name before the file is attempted to be opened. I would have preferred not to have to do that for every single file referenced, but it solves my problem nonetheless.
JakeTheSnake