1

I want to extract all file names in a order as they exist in window explorer. I have googled and found many answers but this did not work. I have a file names in following order:

 video_1
 video_2
 video_3
 video_10
 video_15
 video_20
 video_21

I want in the same order but the result comes in the following form.

 video_1
 video_10
 video_10
 video_2
 video_20
 video_21

I have tried the following codes:

var fileNames= Directory.GetFiles(basePath, "*" + fileExtension) //Not worked
DirectoryInfo info = new DirectoryInfo(basePath);
var filenames = info.GetFiles().OrderBy(p => p.CreationTIme).ToArray(); //Not worked

Can someone tell me how i can do this?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Umer Waheed
  • 4,044
  • 7
  • 41
  • 62
  • well `.OrderBy(p => p.CreationTIme)` wont work as you order them by creation time.. try to sort them with filename.. – Bagus Tesa Apr 17 '17 at 09:54
  • Filename also not working – Umer Waheed Apr 17 '17 at 09:56
  • @Steve - this might be a duplicate question but not of the one you closed with. That one was for VB.Net and thus not an exact dup. Will search to find one in C# – Gilad Green Apr 17 '17 at 10:08
  • The answer is in C# and VB.NET. Let me say however that your solution is faster. – Steve Apr 17 '17 at 10:09
  • Actually Special characters are blocking the required order. so try this DirectoryInfo info = new DirectoryInfo(""); var files = info.GetFiles() .OrderBy(p => p.FullName.ToString("N")); – Sid Apr 17 '17 at 10:17

1 Answers1

3

The reason the order is "incorrect" is because the names are strings and therefore are ordered as strings. What you want is to order by the numeric part of it:

DirectoryInfo info = new DirectoryInfo("");
var files = info.GetFiles()
                .OrderBy(p => p.FullName.Split('_')[0])
                .ThenBy(p => int.Parse(p.FullName.Split('_')[1]));

If you are not sure the format is exactly like that (with a _ and then a valid number) then you can:

Func<string, int, int> parseIntOrDefault = (input, defaultValue) =>
{
    int value = defaultValue;
    int.TryParse(input, out value);
    return value;
};

var result = from file in info.GetFiles()
             let sections = file.FullName.Split('_')
             orderby sections[0], sections.Length == 2 ?
                 parseIntOrDefault(sections[1], int.MaxValue) : int.MaxValue
             select file;
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • If @Umer wants to sort like the file system does I would think this would be a more appropriate answer to his question http://stackoverflow.com/questions/1601834/c-implementation-of-or-alternative-to-strcmplogicalw-in-shlwapi-dll. The actual question contains the code I believe he should use. Although their is nothing wrong with your answer it just doesn't continue to work if he ever changes his format. – Max Young Apr 17 '17 at 10:22
  • @MaxYoung it works for me. Finally. Please write this comment as answer so that i can mark it and other can get help from it – Umer Waheed Apr 17 '17 at 10:32
  • @Umer I cant since the duplicate was accepted. I would just upvote that question. Glad it worked for you. – Max Young Apr 17 '17 at 10:37
  • Thanks again man. – Umer Waheed Apr 17 '17 at 10:52