LastModified Date Comparison is not working as expected

Hi Team,
Need your inputs on the some logic failure.

Working on a logic to Archive/Purge Files from a root folder (Which Includes sub folders)

Steps:

  1. For each File in Folder (Include sub folders)
  2. Get File Info
  3. Compare Current File Created date with Retention Days.
    GetCurFileInfo.LastModifiedDate.ToString < DateTime.Now.AddDays(-1).ToString

Current Year 2024 Files are working fine, but prev year files, the above logic condition is not working.

Logs Example:

Working One:
02/05/2024 11:27:34 => [Info] 01/18/2024 16:20:24 < 02/04/2024 11:27:34
02/05/2024 11:27:34 => [Debug] Archiving the Current File: ‘**C:\Temp\RPA\2024\QTR_1\JAN**FileSetExport__18012024042015.xlsx’.

Not Working One:
02/05/2024 11:27:35 => [Info] 10/11/2023 16:24:46 < 02/04/2024 11:27:35
02/05/2024 11:27:35 => [Debug] Current File: ‘C:\Temp\RPA\2023\QTR_4\OCT\Results.xlsx’ is NOT Qualified to Archive/Purge due to File LastModified Date is:: .10/11/2023 16:24:46

All 2023 Files are not working with the above logic.

please list down the rules for the logic

Retention Days are unclear. E.g. a particular number of days?

We recommend on always compare on DateTime Level and not on string base

Also have a look here:
grafik

Logic to purge/archive files which are older than retention days. For this example, i used 1 day before files to delete. And all these values are configurable.

Used this condition under IF to consider this is old file or a new file.

LastModifiedDate.ToString < DateTime.Now.AddDays(-1).ToString

As mentioned

have a looke here:
grafik

Check also if the Property CreationTime will better match your case

AND

AND the UiPath provided Extensions

EDITED:
fixed wrong Condition suggestion
added Docu link

Don’t compare dates as strings.

Hi @KrishnaKishore

LastModifiedDate returns string. Use date type to compare both

CDate(GetCurFileInfo.LastModifiedDate) < DateTime.Now.AddDays(-1).Date

Another approach is get first all files you want to delete based in purge rule

in assign activity:

array_string_variable = Directory.GetFiles(“folder_path”).Where(Function (x) CDate(new FileInfo(x).LastModifiedDate ) < DateTime.Now.AddDays(-1).Date).ToArray()

Then use For Each to delete the files

DeleteFileBasedPurgeRule.zip (2.8 KB)

Cheers

Thanks Team,
All suggested logic implemented, and it worked.

@KrishnaKishore
Perfect, so the topic can be closed:

Hi,

The issue seems to be related to the comparison logic that you’re using for date comparison in the workflow. You’re converting dates to strings and then comparing the strings, which is not the correct method to compare dates because string comparison is based on lexicographical order, not date order.

Instead of converting dates to strings, you should compare the dates directly. The DateTime objects should be compared without converting them to strings.

Here’s a step by step guide on how you should do the comparison:

  1. For each file in folder: Loop through each file including subfolders.
  2. Get File Info: Use FileInfo to get the details of the file including the LastModifiedDate.
  3. Compare Dates: Compare the LastModifiedDate of the current file directly with the date you get from DateTime.Now.AddDays(-RetentionDays).

Here is what your condition should look like in C#:

if (GetCurFileInfo.LastModifiedDate < DateTime.Now.AddDays(-RetentionDays))
{
// Logic for archiving or purging the file
}

And in UiPath, it would be something like:
Vbscript:

If GetCurFileInfo.LastModifiedDate < DateTime.Now.AddDays(-RetentionDays) Then
’ Your logic for archiving or purging the file
End If

Make sure that RetentionDays is a variable that holds the number of days for retention. For example, if you want to archive files older than 365 days, RetentionDays should be set to 365.

Do not use ToString() for date comparisons. The DateTime objects can be compared directly, which will give you the correct behavior for the comparison regardless of the year.

After you’ve made these changes, your workflow should correctly identify which files need to be archived or purged based on the last modified date compared to your retention period.

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