简体   繁体   中英

Loading in a large amount of files into C# in alpha-numeric order?

I have about 1,500 .txt files of the format "filename-date" where DATE is a number. The files can easily be sorted in ascending order.

Problem is, I want to load all of these 1,500 files into one file, IN ORDER.

I know that a foor loop is in order, but since there are dates which are skipped I don't know how best to go about it.

Assuming that the filename itself is sortable alphabetically, this would work.

  1. Build a list of alphabetically sorted file paths using

     var textFiles = Directory.EnumerateFiles(path, "*.txt").OrderBy(x => new string(x.ToArray())); 
  2. Iterate over your enumerable collection and do your processing work.

     foreach (var textFile in textFiles) { // Do work here } 

If the file names themselves are not sortable, but just the date string at the end is, you might implement a custom comparison, which is explained here .

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