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:
For each File in Folder (Include sub folders)
Get File Info
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.
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.
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:
For each file in folder: Loop through each file including subfolders.
Get File Info: Use FileInfo to get the details of the file including the LastModifiedDate.
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.