How to sort files in a folder based on file name which has datetime stamp

Hello ,

I need to sort the files in a folder based on file name which has datetime stamp.

For example:

I have 4 text files in a folder named

  1. Cart_EY_23032022
    2.Cart_EY_24032022
    3.Cart_EY_01012022
    4.Cart_EY_15022022

I need to sort all the above files based on the datetime stamp which is in filename.
Note: I cannot use file creation date or date modified to sort the files.

Any leads are appreciated.

Hi @Sirimalla_Karthik_Chandra

I’m not sure but try this

Use For Each File in Folder Activity

Filter by → “*.txt”

Order by → Name ascending first

Look into the screenshot

Regards
Gokul

1 Like

you can use

listOfFiles.OrderBy(function(x) System.Text.RegularExpressions.Regex.Match(x, "([\d]{8})").Groups(1).value).ToList

this will sort it by timestamp only so the text etc wont matter

my sequence for reference:
test.xaml (9.4 KB)

image

1 Like

Hi,

Can you try the following expression?

files = System.IO.Directory.GetFiles(yourFolder).OrderBy(Function(f) DateTime.ParseExact(System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"\d{8}$").Value,"ddMMyyyy",System.Globalization.CultureInfo.InvariantCulture)).ToArray

Regards,

1 Like

Hi @Yoichi , Thanks a lot man. The expression given by you is working.

1 Like

Could you Please help me to sort if the file also have a timestamp along with date.

For example: if the file name is
1.Cart_EY_03032022_1501
2.Cart_EY_03032022_1530
3.Cart_EY_03032022_1640

Hi,

The following will work. Can you try?

System.IO.Directory.GetFiles(yourFolder).OrderBy(Function(f) DateTime.ParseExact(System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"\d{8}_\d{4}$").Value,"ddMMyyyy_HHmm",System.Globalization.CultureInfo.InvariantCulture)).ToArray

Regards,

1 Like

Thank you so much for helping me out. @Yoichi

1 Like

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