+1  A: 

The ? character matches "zero or one" characters... so from what you have I would imagine that your search pattern will match any file ending in ".tif" that is between zero and twelve characters long.

Try dropping another file in that is only three characters long with a ".tif" extension and see if the code picks that up as well. I have a sneaking suspicion that it will ;)

As far as the Windows search is concerned, it is most definately not using the same algorithm under the hood. The ? character might have a very different meaning there than it does in the .Net search pattern specification for the Directory.GetFiles(string, string) method.

Josh
+6  A: 

For Directory.GetFiles, ? signifies "Exactly zero or one character." On the other hand, you could use DirectoryInfo.GetFiles, for which ? signifies "Exactly one character" (apparently what you want).

EDIT:

Full code:

string[] resultFileNames = (from fileInfo in new DirectoryInfo(@"C:\temp").GetFiles("????????????.tif") select fileInfo.Name).ToArray();

You can probably skip the ToArray and just let resultFileNames be an IEnumerable<string>.

People are reporting this doesn't work for them on MS .NET. The below exact code works for me with on Mono on Ubuntu Hardy. I agree it doesn't really make sense to have two related classes use different conventions. However, that is what the documentation (linked above) says, and Mono complies with the docs. If Microsoft's implementation doesn't, they have a bug:

using System;
using System.IO;
using System.Linq;

public class GetFiles
{
    public static void Main()
    {
        string[] resultFileNames = (from fileInfo in new DirectoryInfo(@".").GetFiles("????????????.tif") select fileInfo.Name).ToArray();
        foreach(string fileName in resultFileNames)
        {
            Console.WriteLine(fileName);
        }
    }
}
Matthew Flaschen
But 'DirectoryInfo.GetFiles' also returns two items 'c:\temp\GZ96A7005.tif' and 'c:\temp\GZ96A7005001.tif'.
leon ching
Leon, can you post the exact code you're using with DirectoryInfo.GetFiles?
Matthew Flaschen
DirectoryInfo.GetFiles() matches zero or one character for me. .NET 3.5 SP1. Maybe that behavior was a bug in previous versions. Certainly doesn't make sense for them to behave differently.
Lucas
Lucas, I replied in my answer. Please look at the docs I linked.
Matthew Flaschen
You are right that the docs specify different behavior for "?" in both methods. However, I tested this in MS.NET and they both behave the same way (matches zero or one character). It is also this way in Win32 APIs, so it must be a documentation error.
Lucas
A: 
string path = "C:/";
var files = Directory.GetFiles(path)
    .Where(f => f.Replace(path, "").Length == 8);

A little costly with the string replacement. You can add whatever extension you need.

You should use Path.GetFileNameWithoutExtension() instead of Replace()
Lucas
+5  A: 

I know I've read about this somewhere before, but the best I could find right now was this reference to it in Raymond Chen's blog post. The point is that Windows keeps a short (8.3) filename for every file with a long filename, for backward compatibility, and filename wildcards are matched against both the long and short filenames. You can see these short filenames by opening a command prompt and running "dir /x". Normally, getting a list of files which match ????????.tif (8) returns a list of file with 8 or less characters in their filename and a .tif extension. But every file with a long filename also has a short filename with 8.3 characters, so they all match this filter.

In your case both GZ96A7005.tif and GZ96A7005001.tif are long filenames, so they both have a 8.3 short filename which matches ????????.tif (anything with 8 or more ?'s).

UPDATE... from MSDN:

Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".


UPDATE: The MSDN docs specifiy different behavior for the "?" wildcard in Directory.GetFiles() and DirectoryInfo.GetFiles(). The documentation seems to be wrong, however. See Matthew Flaschen's answer.

Lucas
Thanks very much:)
leon ching