DirectoryInfo.GetFiles Method

  • Thread starter Thread starter Demetri
  • Start date Start date
D

Demetri

Using the GetFiles method of the DirectoryInfo instance
one can pass in a search pattern of string type.

For example:
DirectoryInfo di = new DirectoryInfo("C:\temp");
FileInfo[] fi = di.GetFiles("*.doc");

That code would return all the files with the doc
extension in the C:\temp folder. No problem.

However, suppose I want all the files that have the
word "Corporate" and "Audit" and "finance" in the files
name (not the files contents)?

This means if there are two files:
1. System finance.doc
2. Audit Finance Corporate.doc

The above should return just file number 2 since it
satisfies the requirement to have those three words in the
files name.

Any ideas on how to implement this?
 
Hi,

AFAIK there is not a framework method for this, GetFiles expect a string
not a regex, solution:
Return all the files using GetFiles() and then filter them by yourself
using a regex or using FileInfo.Name.LastIndexOf method, this is not a good
solution , though . so I will go for the regex.

Hope this help,
 
The Xceed FileSystem that comes with Xceed Zip for .NET enables you to do
stuff like this (and much more!):

DiskFolder folder = new DiskFolder( "c:\\temp" );
DiskFile[] files = folder.GetFiles( true, new AndFilter( "*corporate*",
"*audit*", "*finance*" ) );

Already implemented filters are: NameFilter (or passing a string),
AttributeFilter (or passing a FileAttribute), DateFilter, SizeFilter,
AndFilter, OrFilter, NotFilter. You can also derive from the base Filter
class and implement your own!

http://www.xceedsoft.com/products/ZipNet/


Martin Plante
Xceed Software Inc.
http://www.xceedsoft.com
 
Back
Top