-1

I have a folder which contains only .txt files. I want my output to have a certain order.

The first 2 digit of every filename is its unique ID. I want to order the files numerically in that order

I've researched multiple questions that seem similar to this like Getting all file names from a folder using C#

but none has helped me accomplish this goal. What is the easiest way for me to get my desired output?

        string folder = @"C:\Testing\";
        var txtFiles = Directory.GetFiles(folder, "*.txt")
                                 .Select(Path.GetFileName)
                                 .ToList();
        foreach(var f in txtFiles)
        {
            Console.WriteLine(f);
        }
        Console.ReadKey();

Windows Explore

enter image description here

Console Output

Output file

taji01
  • 2,527
  • 8
  • 36
  • 80
  • Use a sort with your own comparison function - https://stackoverflow.com/questions/1832684/c-sharp-sort-and-orderby-comparison – Dave S May 28 '19 at 21:53
  • 1
    Possible duplicate of [Sorting an array of folder names like Windows Explorer (Numerically and Alphabetically) - VB.NET](https://stackoverflow.com/questions/3099581/sorting-an-array-of-folder-names-like-windows-explorer-numerically-and-alphabet) – Xiaoy312 May 28 '19 at 22:05

4 Answers4

2

This pads the first number section with zeroes for sorting purposes:

string folder = @"C:\Testing";
var txtFiles = Directory.GetFiles(folder, "*.txt")
                        .Select(Path.GetFileName)
                        .OrderBy(f => f.Split('_')[0].PadLeft(5, '0'));

foreach (var f in txtFiles)
{
    Console.WriteLine(f); 
}
Martin
  • 87
  • 1
  • 5
1

You can just use a OrderBy linq statement and parse out the file name to get what you want. Also you don't need ToList() if all you want is a simple list such as IEnumerable.

For example:

var txtFiles = Directory.EnumerateFiles(folder, "*.txt")
                         .Select(Path.GetFileName)
                         .OrderBy(file =>
                         {
                             string[] nameParts = file.Split('_');
                             if (nameParts.Length > 0)
                             {
                                 int sortValue;
                                 if (int.TryParse(nameParts[0], out sortValue))
                                 {
                                     return sortValue;
                                 }
                             }
                             return 0;
                         });
Mr. Young
  • 2,364
  • 3
  • 25
  • 41
1

Easier alternative can be to use padding :

var txtFiles = Directory.EnumerateFiles(folder, "*.txt")
                        .Select(Path.GetFileName)
                        .OrderBy(s => s.PadLeft(11));
Slai
  • 22,144
  • 5
  • 45
  • 53
-1

If you want it in order you better sort it. using .OrderBy extension method.

string folder = @"C:\Testing\";
var txtFiles = Directory.GetFiles(folder, "*.txt")
               .Select(Path.GetFileName)
               .OrderBy(x => x)
               .ToList();
foreach (var f in txtFiles)
{
 Console.WriteLine(f);
}
Console.ReadKey();
Bibash
  • 67
  • 8