简体   繁体   中英

Directory.GetFiles get multiple files using filePattern

How do I setup pattern to return multiple files, but only such that I am looking for.

I have a directory which contains files:

marks v1.csv,
marks_east.csv
marks.csv
marks_west.csv
marks23.csv
marks24.csv

I need marks.csv, marks_east.csv, marks_west.csv using filePattern .

Earlier I was using exact file name as I was interested in only one file. Now I want to reuse same process by modifying filePattern .

var files = Directory.GetFiles(sourceDirectory, filePattern).OrderBy(d => new FileInfo(d).CreationTime);

Directory.GetFiles supports wildcards.

var files = Directory.GetFiles(sourceDirectory, "marks_*.csv").OrderBy(d => new FileInfo(d).CreationTime);

This will not catch marks.csv though. Regex would be very well suited for this, but alas the GetFiles function does not support regular expressions. A quick (a bit hacky) solution might be:

var files = Directory.GetFiles(sourceDirectory, "marks_*.csv");
if (File.Exists(sourceDirectory+"marks.csv"))
    files.Insert(0, "marks.csv");
files.OrderBy(d => new FileInfo(d).CreationTime);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM