Reputation: 1073
I'm working on a batch program that process a big amount of files (more than 50 000 files) and I'm facing weird behavior with the Directory.GetFiles
method.
In the process I move files that matches the following search pattern "*.pdf", and get the files thanks to the Directory.GetFiles
method :
I was very surprised to see that sometimes I have .pdfa files moved.
So I've checked the doc and it clearly states that if the search pattern contains an extension with 3 letters every files that have an extension that begins with the extension will be returned.
I've tested with a simple program and It does not behave like stated in the doc, it only behaves like this in very rare occasion.
With this code :
static void Main(string[] args) {
var directory = @"E:\Test\";
var files = Directory.GetFiles(directory, "*.pdf");
foreach(var file in files)
Console.WriteLine(file);
}
I have this result :
Do you have any explanation about this behavior ?
Upvotes: 1
Views: 1135
Reputation: 1073
As explained by @luaan and by @hans-passant (thanks a lot !) I do not found the file with the .pdfa extension, because the 8.3 format is disabled on my hard drive.
On a hard drive with the 8.3 format enabled, the method behaves like stated in the doc.
The GetFiles has a different behavior with the setting enabled or not.
Upvotes: 1
Reputation: 62488
This is the expected behavior of the GetFiles method and it's same on Windows as well, if you search in directory with .pdf it will pick files with extensions .pdfa or *.pdfaaa, you would need to put a Where()
yourself like:
Directory.GetFiles(directory, "*.pdf").Where(item => item.EndsWith(".pdf"));
As you can see that when we search in windows It is giving the same result as your code was giving:
For the reason of why the GetFiles is behaving that way please have a look here and you might also want to look at this post as well
Upvotes: 6