Search for files with no extension

G

Guest

Hi,

I need to search a particular directory for all the files that do not have
any extension and have a specific naming convension. The first 3 characters
of the file name are alpha and the rest are numeric. For example, valid files
are 'abc1234', 'xyz9876' etc. Is there a search pattern I can use with the
Directory.getFiles( ) method. Thanks in Advance

Prem
 
K

Kai Brinkmann [MSFT]

I'm afraid Directory.GetFiles() isn't going to help you here since it only
supports two wildcards: * (one or more characters) and ? (exactly one
character). It seems to me you are going to need a regular expression to
handle the file name matching. Here's what I would do:

1. Retrieve all files in the directory using Directory.GetFiles(string
directory)

2. Parse the file list to identify all files with no extension. You can use
the FileInfo class. Whenever FileInfo.Extension returns an empty string, you
have found a file.

3. Compare the file names with no extension (FileInfo.Name) against a
regular expression. Something like [A-Za-z][A-Za-z][A-Za-z]\d+ should work.
This will look for three characters in the A-Z range (both cases) followed
by one or more decimal characters. I suppose you could go with just [a-z] if
you used FileInfo.Name.ToLower()

You're going to need the System.Text.RegularExpressions namespace for this
approach. First, create an expression:

Regex r = new Regex("[a-z][a-z][a-z]\d+", RegexOptions.Compiled);

After that you can use:

Match m = r.Match(FileInfo.Name.ToLower());

and test m.Success to see if you found a match.

I hope this helps.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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