Sort directory files and get the highest file name

I have a directory with 40 files with names from 0 to 39 (for example), I'm trying to get the file with the largest number in its name (which means I need to get "39") I'm trying to sort the directory. I tried using the following topics:

How to get a list of files in a directory sorted by name

Sorting Directory.GetFiles result in C #

Nothing works for me .. I tried each of the methods - using Linq and others. and I do not know why..

I get the following sorting result (see image below): enter image description here

Thanks for the help,

Dean Bracha.

+6
source share
3 answers

It is logical that they would be sorted in this way, you would introduce semantics to sort it by number, namely, analyze all file names by numbers, and then sort files by these parameters.

Sort of

files.OrderBy(path => Int32.Parse(Path.GetFileNameWithoutExtension(path))) 

Use Last() to get the file with the highest number.

+6
source

This is VB.NET for the highest numbered name. Changing the OrderByDescending key to x.LastWriteTime gets the newest file.

  Dim OldName As String = String.Empty Dim DI As New IO.DirectoryInfo("C:\") For Each FI As IO.FileInfo In DI.GetFiles("*.*").OrderByDescending(Function(x) x.Name) OldName = FI.FullName Exit For Next 
0
source

The windows have a built-in function StrCmpLogicalW , which will compare strings in numbers instead of letters. It is easy to make a mapper that calls this function and uses it for comparison.

 public class StrCmpLogicalComparer : Comparer<string> { [DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)] private static extern int StrCmpLogicalW(string x, string y); public override int Compare(string x, string y) { return StrCmpLogicalW(x, y); } } 

Here is an example program that shows the difference between default sorting and StrCmpLogicalW sorting

 class Program { static void Main() { List<string> items = new List<string>() { "Example1.txt", "Example2.txt", "Example3.txt", "Example4.txt", "Example5.txt", "Example6.txt", "Example7.txt", "Example8.txt", "Example9.txt", "Example10.txt", "Example11.txt", "Example12.txt", "Example13.txt", "Example14.txt", "Example15.txt", "Example16.txt", "Example17.txt", "Example18.txt", "Example19.txt", "Example20.txt" }; items.Sort(); foreach (var item in items) { Console.WriteLine(item); } Console.WriteLine(); items.Sort(new StrCmpLogicalComparer()); foreach (var item in items) { Console.WriteLine(item); } Console.ReadLine(); } } 

which outputs

 Example1.txt Example10.txt Example11.txt Example12.txt Example13.txt Example14.txt Example15.txt Example16.txt Example17.txt Example18.txt Example19.txt Example2.txt Example20.txt Example3.txt Example4.txt Example5.txt Example6.txt Example7.txt Example8.txt Example9.txt Example1.txt Example2.txt Example3.txt Example4.txt Example5.txt Example6.txt Example7.txt Example8.txt Example9.txt Example10.txt Example11.txt Example12.txt Example13.txt Example14.txt Example15.txt Example16.txt Example17.txt Example18.txt Example19.txt Example20.txt 
0
source

Source: https://habr.com/ru/post/921629/


All Articles