How to Fetch files from a folder based on the timestamp in filename?

Hello All,

I save the Last Run Date Time in sql server and retrieve the datetime value everytime for a new trigger.

I need to get the files from the source location only if the filename timestamp is greater than Last run date time.
LastRunDateTime = “20220313”
The file name will be as follows:

  1. Auth_released_20220314_1706
    2.Auth_released_20220315_1508

I need to compare only the date from Filename with LastRunDateTime, and only if the filename date is greater than LastRunDatetime. Then I have to Copy all those files to another location.

Any helps and leads are much appreciated.

2 Likes

Hi

Hope the below steps would help you resolve this

  1. Use a assign activity like this

arr_filepaths = Directory.GetFiles(“yourfolderpath”)

Where arr_filepaths is a variable of type array of strings

  1. Let’s take like you have your last run Datetime in a string variable named strtime

  2. Now use a FOR EACH activity and pass the above array variable as input and change the type argument as string

  3. Inside the loop use a IF condition like this

Datetime.ParseExact(Split(Path.GetFileNameWithoutExtension(item.ToString), “_”)(1).ToString.Trim, “yyyyMMdd”, System.Globalization.CultureInfo.InvariantCulture) > Datetime.ParseExact(strtime.ToString.Trim “yyyyMMdd”, System.Globalization.CultureInfo.InvariantCulture)

If true it goes to then block where use a Move file activity and in source mention as item.ToString and in destination mention your folder path

Cheers @Sirimalla_Karthik_Chandra

Hi @Sirimalla_Karthik_Chandra
You can load the records into an array variable with code.

Directory.GetFiles("c:\temp\")

Then you can do a search in your array variable, doing a split to get the date snippet and compare with the current date, following example code.

aryFile.Where(function(x) date.ParseExact(New FileInfo(x).Name.Split("_"c)(2).ToString, "yyyyMMdd", Nothing) >= Now.Date)

So for your project, I believe it would look like this.

aryFile.Where(function(x) date.ParseExact(New FileInfo(x).Name.Split("_"c)(2).ToString, "yyyyMMdd", Nothing) >= date.ParseExact(LastRunDateTime, "yyyyMMdd", Nothing))
1 Like

Hi @Sirimalla_Karthik_Chandra

you can try like this

Goto Imports-> and search for System.Globalization and System.Text.RegularExpressions


  • For each file in folder
    • If DateTime.ParseExact(“LastRunDate”,“yyyyMMdd”,CultureInfo.InvariantCulture) > DateTime.ParseExact(Regex.Match(Path.GetFileNameWithoutExtension(CurrentFile.ToString),"(?<=_)\d+(?=_)").ToString,“yyyyMMdd”,CultureInfo.InvariantCulture)
      • True-> Copy file Source will be CurrentFile.Tostring

Here is the sample SS and flow
image

Sequence.xaml (8.8 KB)

Hope this Helps!!

Regards
Sudharsan

Hi @Palaniyappan ,Thank you for replying to my post.

I get the following error in the IF

Can you please help me with the error.

Hi @Sudharsan_Ka , Thanks for replying to my post.

The source folder has lots of other files, Hence I’m only filtering for specific filename and then I have to only copy the files which are greater than lastrundate from source folder to inputFolder.

For each file in Folder does not work for my usecase.

Can you suggest me other solution.

Have you Checked the flow\Skeleton which i sent @Sirimalla_Karthik_Chandra

Because I have included the condition as well

Try that and let me know if you need further assistance

Regards
Sudharsan

I opened the xaml file shared by you, However there was a package issue.

image

@Sirimalla_Karthik_Chandra

Simply upgrade your all packages or check the package and compare the necessary packages with the dependencies here
image

Regards
Sudharsan

I not allowed to use Balareva packages in my project. Can you please share me the xaml file with only required packages if possible.

That’s used in my project you can check only the dependencies you want

Upgrade your Uipath.System.Activities package

Regards
Sudharsan

No luck, I had updated all the packages. Still the same issue.

Okay @Sirimalla_Karthik_Chandra

So let’s create a new workflow

  • Search for For each file in folder and drag them in
    • If and give the condition I mentioned above
      DateTime.ParseExact(LastRunDate,“yyyyMMdd”,CultureInfo.InvariantCulture) > DateTime.ParseExact(Regex.Match(Path.GetFileNameWithoutExtension(CurrentFile.ToString),"(?<=)\d+(?=)").ToString,“yyyyMMdd”,CultureInfo.InvariantCulture)

Where LastRunDate = Your mentioned date

Regards
Sudharsan

1 Like

@Sudharsan_Ka , I have found the solution myself. Thank you so much for your effort.

my solution is as follows:

Taking assign Getfiles=directory.getfiles(FolderPath,“Auth_released*.txt”)
second assign lastdate = “20220313”
taking for each and assigning Getfiles as input
Inside Foreach dragging IF Activity and passing the below expression
DateTime.ParseExact(System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(item),"\d{8}_\d{4}$").Value,“yyyyMMdd_HHmm”,System.Globalization.CultureInfo.InvariantCulture) >date.ParseExact(lastdate.ToString,“yyyyMMdd”,System.Globalization.CultureInfo.InvariantCulture)
So this way my issue got resolved.

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