Reputation: 245
I have some code that is meant to get files in a directory, which is simple enough
foreach (var Totalfile in new DirectoryInfo(rootfolder).GetFiles("*Totals*.csv", SearchOption.TopDirectoryOnly))
The files are named as follows:
Totals.CSV142344
Totals.CSV142409
Totals.CSV142433
Totals.CSV142501
Totals.CSV142528
My issue is that it is not picking up the last file.
I have fixed the code by putting this instead:
foreach (var Totalfile in new DirectoryInfo(rootfolder).GetFiles("*Totals*.csv*", SearchOption.TopDirectoryOnly))
Saying get any files that contains both Totals and .csv, with anything after the .csv.
What I don't get is why it got the top four files, but not the bottom.
I'd have thought none of the files would be picked up by the original code?
Upvotes: 23
Views: 1269
Reputation: 2685
Apparently GetFiles
adheres to the pattern matching logic as it is implemented in say the dir
command.
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".
As @GSerg pointed some of your files have a matching 8.3 name.
dir /x *Totals*.csv*
2020-01-31 09:33 0 TOTALS~1.CSV Totals.CSV142344
2020-01-31 09:33 0 TOTALS~2.CSV Totals.CSV142409
2020-01-31 09:33 0 TOTALS~3.CSV Totals.CSV142433
2020-01-31 09:33 0 TOTALS~4.CSV Totals.CSV142501
2020-01-31 09:33 0 TO5404~1.CSV Totals.CSV142528
Try changing the pattern to Totals.csv*
to match all files.
Upvotes: 20