Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory.GetFiles doesn't pick up all files

Tags:

c#

filenames

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?

like image 535
zedgraphsad Avatar asked Jan 31 '20 08:01

zedgraphsad


People also ask

How to get all file from folder in uipath?

Open the variables panel and change the type. Add a For Each activity and iterate through the list of files and print their path on the console using the Writeline activity. You will have to change the item/file to string in order to print. Your complete flow looks like this.

What does directory GetFiles return?

GetFiles(String, String, SearchOption) Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.


1 Answers

Apparently GetFiles adheres to the pattern matching logic as it is implemented in say the dir command.

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".

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.

like image 73
user1859022 Avatar answered Sep 23 '22 15:09

user1859022