Sort files by files name

Hi! i’m trying to get file’s name sorted by name ascending. in this case i have files with name :
RPA_020822_1.txt
RPA_020822_5.txt
RPA_010922_1.txt
RPA_020822_11.txt

the format name file is RPA_DDMMYY_x.txt (x is sequence). i’ve already sorting by datetime but actual result was :
RPA_010922_1.txt
RPA_020822_1.txt
RPA_020822_11.txt
RPA_020822_5.txt

My Expected result was :

RPA_010922_1.txt
RPA_020822_1.txt
RPA_020822_5.txt
RPA_020822_11.txt

by the way, i using this expresion inside a for each loop

Directory.Getfiles(mypath, “RPA*.txt”).OrderBy(Function(x) DateTime.ParseExact(system.text.RegularExpressions.Regex.Match(Path.GetFileNameWithoutExtension(x), "\d{6}\d{1,}“).Value.ToString.Split(”"c)First, “ddMMyy”, System.Globalization.CultureInfo.InvarianCulture))

Hi,

Can you try the following sample?

image

files = Directory.Getfiles(mypath, "RPA*.txt").OrderByDescending(Function(x) DateTime.ParseExact(System.Text.RegularExpressions.Regex.Match(Path.GetFileNameWithoutExtension(x), "\d{6}(?=_\d+$)").Value, "ddMMyy", System.Globalization.CultureInfo.InvariantCulture)).ThenBy(Function(f) Int32.Parse(System.Text.RegularExpressions.Regex.Match(Path.GetFileNameWithoutExtension(f), "\d+$").Value)).ToArray

Sample20220812-5.zip (3.2 KB)

Regards,

it work but i change it in the text of “\d+$”, because regex value of it make result “” and it can’t be convert to integer

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.