Directory.GetFiles() Problem

J

Julie Smith

Hi,
Is it just me or does the search pattern parameter in Directory.GetFiles()
have a problem with the '?' character?

'*.*' works to find all files, but '?.*' does not work to find all files
that have only a one letter (and infinite extension size) filename. In fact,
it seems to act the same as '*.*'...

Is this by design?

Thanks in advance.
 
A

Alex Meleta

Hi Julie,

No, it's not by design, it should work. How do you use it coz that should
work: Directory.GetFiles(<path>, "*.tx?") // or "?n.*"

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



JS> Hi,
JS> Is it just me or does the search pattern parameter in
JS> Directory.GetFiles()
JS> have a problem with the '?' character?
JS> '*.*' works to find all files, but '?.*' does not work to find all
JS> files that have only a one letter (and infinite extension size)
JS> filename. In fact, it seems to act the same as '*.*'...
JS>
JS> Is this by design?
JS>
JS> Thanks in advance.
JS>
 
D

Daniel Bass

What version of .Net are you using? I've just tried this in .Net 2.0 (VS
2005) and it works as expected... Only files that had exactly one character
before the point (.) were listed.

Dim listOfFiles As String() = Directory.GetFiles("C:\", "?.*")
For Each filename As String In listOfFiles
Console.WriteLine(filename)
Next
 
J

Julie Smith

I have constructed a string pattern for each day (DDMMYYxx.txt). So, for
example the string pattern for today is "110707xx.txt". The search string I
use is "110707??.txt". However, it picked up a file called "1107071.txt". As
you can see, that only fills one ?, not both.

I find this very weird. I might do some test scripts to see if I can
reproduce it outside my application.
 
J

Julie Smith

Well, I ran two simulations:
string[] files = Directory.GetFiles(dir, "100707??.cnc",
SearchOption.AllDirectories);
foreach (string s in files)
Console.WriteLine(s);

This returned:
\1007070.CNC
\10070701.CNC
\10070702.CNC
\10070703.CNC
\10070704.CNC
\10070705.CNC
\10070706.CNC
\10070707.CNC
\1007071.CNC
\1007072.CNC

As you can see, its not working as expected... However, changing the search
string to "100707?.cnc":
string[] files = Directory.GetFiles(dir, "100707?.cnc",
SearchOption.AllDirectories);

This produces:
\1007070.CNC
\1007071.CNC
\1007072.CNC

This works as expected.

This is .NET 2.0 btw :)
 
J

Julie Smith

It is .net 2.0. My previous post to Alex shows my problem with it not
working when two ? are used together. Referring to your example, what if
your search string was "??.*", would it only list files with TWO characters
before the period?
 
J

Jon Skeet [C# MVP]

Julie Smith said:
I have constructed a string pattern for each day (DDMMYYxx.txt). So, for
example the string pattern for today is "110707xx.txt". The search string I
use is "110707??.txt". However, it picked up a file called "1107071.txt". As
you can see, that only fills one ?, not both.

I find this very weird. I might do some test scripts to see if I can
reproduce it outside my application.

It's behaving exactly as documented. MSDN says that the wildcard ?
matches "Exactly zero or one character" - so it *should* match
1107071.txt.
 
J

Julie Smith

Thank you Jon. I guess I need to filter the files by string length now in
order to get my required result.
 
R

Rad [Visual C# MVP]

Hi,
Is it just me or does the search pattern parameter in Directory.GetFiles()
have a problem with the '?' character?

'*.*' works to find all files, but '?.*' does not work to find all files
that have only a one letter (and infinite extension size) filename. In fact,
it seems to act the same as '*.*'...

Is this by design?

Thanks in advance.

I believe it is by design. Clearly you're an old hand at the DOS
prompt where ? meant a single character as opposed to .NET where it
means zero or one
 
H

hello

I wrote a simple test program, and I found that ?? works like *. I dont know why.

If ? means 0 or 1 character, but why did files ending with more than 2 characters show in the results, when I searched for something like "abc??.txt" in the directory? I did not work as doc.
 
T

Tim Roberts

hello said:
I wrote a simple test program, and I found that ?? works like *. I
dont know why.

If ? means 0 or 1 character, but why did files ending with more than
2 characters show in the results, when I searched for something like
"abc??.txt" in the directory? I did not work as doc.

Can you be more precise about exactly what you tried, and exactly what you
expected? Include a "dir" so we can see what the directory looked like. I
just tried an example, where ? and ?? did exactly what they were supposed
to.

Also, see if the "dir" command provides the same results with the same
glob.
 

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