tags:

views:

586

answers:

5

Directory.GetFiles(LocalFilePath, searchPattern);

MSDN Notes: When using the asterisk wildcard character in a searchPattern, such as ".txt", the matching behavior when the extension is exactly three characters long is different than when the extension is more or less than three characters long. A searchPattern with a file extension of exactly three characters returns files having an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files having extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, while a search pattern of "file.txt" returns both files.

The following list shows the behavior of different lengths for the searchPattern parameter:

*

  "*.abc" returns files having an extension of .abc, .abcd, .abcde, .abcdef, and so on.
*

  "*.abcd" returns only files having an extension of .abcd.
*

  "*.abcde" returns only files having an extension of .abcde.
*

  "*.abcdef" returns only files having an extension of .abcdef.

With the searchPattern parameter "*.abc", How can I return files having an extension of .abc, not .abcd, .abcde and so on ?

Maybe this function will work:

    private bool StriktMatch(string fileExtension, string searchPattern)
    {
        bool isStriktMatch = false;

        string extension = searchPattern.Substring(searchPattern.LastIndexOf('.'));

        if (String.IsNullOrEmpty(extension))
        {
            isStriktMatch = true;
        }
        else if (extension.IndexOfAny(new char[] { '*', '?' }) != -1)
        {
            isStriktMatch = true;
        }
        else if (String.Compare(fileExtension, extension, true) == 0)
        {
            isStriktMatch = true;
        }
        else
        {
            isStriktMatch = false;
        }

        return isStriktMatch;
    }

Test Program:

class Program
{
    static void Main(string[] args)
    {
        string[] fileNames = Directory.GetFiles("C:\\document", "*.abc");

        ArrayList al = new ArrayList();

        for (int i = 0; i < fileNames.Length; i++)
        {
            FileInfo file = new FileInfo(fileNames[i]);
            if (StriktMatch(file.Extension, "*.abc"))
            {
                al.Add(fileNames[i]);
            }
        }

        fileNames = (String[])al.ToArray(typeof(String));

        foreach (string s in fileNames)
        {
            Console.WriteLine(s);
        }

        Console.Read();
    }

Anybody else better solution?

+4  A: 

Not a bug, perverse but well-documented behavior. *.doc matches *.docx based on 8.3 fallback lookup.

You will have to manually post-filter the results for ending in doc.

Joshua
Deleted my answer as you are right.
Chris Lively
http://msdn.microsoft.com/en-us/library/wz42302f.aspx"When * wildcard in a searchPattern, the matching behavior when the extension is exactly 3 chars is different than when the extension is more or less than 3 chars."How irritating.
Kevin Haines
good to know. thx!
jms
+3  A: 

The answer is that you must do post filtering. GetFiles alone cannot do it. Here's an example that will post process your results. With this you can use a search pattern with GetFiles or not - it will work either way.

List<string> fileNames = new List<string>();
// populate all filenames here with a Directory.GetFiles or whatever

string srcDir = "from"; // set this
string destDir = "to"; // set this too

// this filters the names in the list to just those that end with ".doc"
foreach (var f in fileNames.All(f => f.ToLower().EndsWith(".doc")))
{
    try
    {
        File.Copy(Path.Combine(srcDir, f), Path.Combine(destDir, f));
    }
    catch { ... }
}
ctacke
+1  A: 

use linq....

    string strSomePath = "c:\\SomeFolder";
string strSomePattern = "*.abc";
string[] filez = Directory.GetFiles(strSomePath, strSomePattern);

var filtrd = from f in filez
      where f.EndsWith( strSomePattern )
      select f;

foreach (string strSomeFileName in filtrd)
{
 Console.WriteLine( strSomeFileName );
}
Muad'Dib
A: 

This won't help in the short term, but voting on the MS Connect post for this issue may get things changed in the future.

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=95415

Martin Brown
A: 

Since for "*.abc" GetFiles will return extensions of 3 or more, anything with a length of 3 after the "." is an exact match, and anything longer is not.

string[] fileList = Directory.GetFiles(path, "*.abc");

foreach (string file in fileList)
{
   FileInfo fInfo = new FileInfo(file);

   if (fInfo.Extension.Length == 4) // "." is counted in the length
   {
      // exact extension match - process the file...
   }
}

Not sure of the performance of the above - while it uses simple length comparisons rather than string manipulations, new FileInfo() is called each time around the loop.

Kevin Haines