RegEx and filenames

  • Thread starter Thread starter Jeff Williams
  • Start date Start date
J

Jeff Williams

I have a list of file names I need to parse and check if they match a
valid expression.

I want this to work like you were listing a directory.

ie

*.doc dhows all doc
*.* shows all documents
*doc shows all those ending with doc
?abc.doc would show documents aabc.doc, babc.doc etc

etc
 
Jeff said:
I have a list of file names I need to parse and check if they match a
valid expression.

I want this to work like you were listing a directory.

ie

*.doc dhows all doc
*.* shows all documents
*doc shows all those ending with doc
?abc.doc would show documents aabc.doc, babc.doc etc

This might be of some interest to you:
http://www.codeproject.com/cs/files/FileSystemEnumerator.asp

If you're just interested in the regex stuff, this is roughly what you need:

// trim whitespace off file spec and convert Win32 wildcards to
regular expressions
string pattern = spec
.Trim()
.Replace(".", @"\.")
.Replace("*", @".*")
.Replace("?", @".?")
;

Regex re = new Regex("^" + pattern + "$", RegexOptions.IgnoreCase);

-cd
 
Use the System.IO.Directory.GetFiles(string, string) method. The first
parameter is the path, and the second is the search pattern, which works
just like the file system.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Printing Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 

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

Back
Top