Get a collection of files whose names match a regular expression?

C

Chrisso

Hi All

Is it possible with the Microsoft Scripting Runtime library to get a
collection of all the files in a folder that match a regular
expression?

That is, can I make a call through the FileSystemObject or a Folder
object to get just the files that start with the letter 'A' for
example?

The only API I can see is Folder.Files which gives *all* the files
then I would have to sort through and do the pattern matching myself
which seems like it will be very slow. Any ideas?

Thanks in advance,
Chrisso
 
R

rowe_newsgroups

Hi All

Is it possible with the Microsoft Scripting Runtime library to get a
collection of all the files in a folder that match a regular
expression?

That is,  can I make a call through the FileSystemObject or a Folder
object to get just the files that start with the letter 'A' for
example?

The only API I can see is Folder.Files which gives *all* the files
then I would have to sort through and do the pattern matching myself
which seems like it will be very slow. Any ideas?

Thanks in advance,
Chrisso

You can use System.IO.Directory.GetFiles to do a much better search
(with wildcards), however if you wanted to do a Regex search you'd
have to loop through the files and do a Regex.IsMatch on each file,
which would be very, very slow. Your other option, which wouldn't
truly be Regex, would be to create a LINQ query that represented the
Regex pattern and use it. LINQ should be much faster than using
Regex.IsMatch.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
C

Chrisso

Thanks Seth

I will try out System.IO.Directory.GetFiles as wildcard matching is
fine.

I am actually wrting my VB code in Excel VBA - do I need to include a
reference to access this functionality?

Chrsso
 
R

rowe_newsgroups

Thanks Seth

I will try out System.IO.Directory.GetFiles as wildcard matching is
fine.

I am actually wrting my VB code in Excel VBA - do I need to include a
reference to access this functionality?

Chrsso

Wow, I completely missed that you weren't using .NET. Now that I see
you aren't, I have to send you to the excel programming group since we
can't be of help to you here (Excel VBA is in no way VB.NET)

http://groups.google.com/group/microsoft.public.excel.programming/topics?lnk

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
C

Chrisso

Thankyou. I will give it a whirl.

I posted here as I thought the topic was independent of Excel. MS is
weird.

Chrisso
 
R

rowe_newsgroups

Thankyou. I will give it a whirl.

I posted here as I thought the topic was independent of Excel. MS is
weird.

Chrisso

The topic is independent of Excel, but it's not from VBA. Let me
rephrase my earlier statement:

VBA is in no way Visual Basic .NET

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top