Get file by order by date?

I have file in folder 4 file as below.

image

If round 1 , I want to get file that lowest (read date after _ ) Ex AAA_20211010.xlsx
round2 , get file = BAS_20211010.xlsx
round3, get file = AAA_20211011.xlsx
round4, get file = AAA_20211012.xlsx

Please guide me about it.

You can get the files in an array using

Directory.getFiles(“Folderpath”,"*.txt")

then loop through the items using for loop,

split the file name using Split(item. toString,"_"), store it in strDate

and then convert the date string in date format,

Datetime.ParseExact(strDate,yyyMMdd, System.Globalization.CultureInfo.InvariantCulture)

then you can compare and fetch the oldest date and the file corresponding to it

for this, you may use a function on datatable or an IF condition

store the first item in the variable, dateOldest
then as the loop iterates, just compare it the current item’s date is older than dateOldest. You can use normal < to compare dates.

after the loop ends you’ll have the oldest date
and you can open the file with that date

Hi,

Hope the following helps you.

files = System.IO.Directory.GetFiles(yourFolder,"*.xlsx").OrderBy(Function(s) System.Text.RegularExpressions.Regex.Match(s,"\d+(?=\.xlsx$)",System.Text.RegularExpressions.RegexOptions.IgnoreCase).Value).ToArray

Sequence1.xaml (6.7 KB)

Regards,

2 Likes

Hi

I feel like when file is created with datetime included in its filename it would have been added or created or moved to the folder at specific time that is considered as CreationTime

So if we can sort with the CreationTime it would probably give the order of files on lowest date as well

Use a assign activity like this

arr_filepath = Directory.GetFiles(“yourfolderpath”,*.xlsx”).OrderBy(Function(a) New Fileinfo(a).CreationTime).ToArray()

Cheers @Maria99

1 Like

@Maria99
we do understand that you are looking for are ordering taking 2 criterias into account

  • first token till the underscore
  • date information within filename

Ensure following:
grafik

Flow:
grafik

(From fi In New DirectoryInfo("C:\_demo\Subfolder").GetFiles()
Let prfx = Regex.Match(fi.Name, "^.*(?=_)").toString
Let sd = Regex.Match(fi.Name, "\d{8}").toString
Let dtp = DateTime.ParseExact(sd,"yyyyMMdd", CultureInfo.InvariantCulture)
Order By dtp,prfx
Select f=fi.FullName).toarray

Line1: readin all files for looping
Line2: Extract the prefix part till _
Line3: Extract the date part
Line4: parse the date string into a Datetime
Line5: Order on prefixpart, date
Line6: Extract the FullFilePath
return a String Array

We are working with DirectoryInfo / FileInfo classes as we do benefit that different information are available with less code (e.g. FileName only)

Insights

2 Likes