How to delete the old excel file from a folder

Hi All,

I want to delete the excel files from a folder which contains more than 4days old
for example I should keep the excel till 28-4=24/09/2022, before 24/09/2022 excel I need to delete.
the excels contains inside the folder :
“\10.1.2.82\Software Data\Automation\NaveenKumarS\ModmedInventory\ModmedRejection28092022”.

Please do needful.

@ushu
@Palaniyappan
@Gokul001
@Rahul_Unnikrishnan
@Yoichi
@ppr
@Lakshmi_Sai
@lakshman

Is it a File name or folder name?

That is folder name which will create daily on basis of current date

inside that folder excels are present

Hi @HeartCatcher

Check out the XAMl file

DeleteFile.xaml (5.7 KB)

Regards
Gokul

Here is the one line code, using Invoke code you can achieve it

array.ForEach(Directory.GetFiles(“YourFolderPath”).Where(Function(x) New FileInfo(x).LastWriteTime.Date < Today.Date.AddDays(-26)).ToArray,Sub(x) File.Delete(x))

If are would like to proceed with regular method…take the below code and paste it in the For each and then use Delete activity…

Directory.GetFiles(“YourFolderPath”).Where(Function(x) New FileInfo(x).LastWriteTime.Date < Today.Date.AddDays(-26))

Hi @HeartCatcher

Loop each excel files from folder
Directory.GetFiles(“\10.1.2.82\Software Data\Automation\NaveenKumarS\ModmedInventory\ModmedRejection28092022”,“*.xlsx”)

in if condition (Directory.GetCreationTime(item.ToString).ToShortDateString.CompareTo(now.date.AddDays(-4).ToShortDateString))=-1

Filepath=\10.1.2.82\Software Data\Automation\NaveenKumarS\ModmedInventory
Inside above path there are 4 folders:
ModmedRejection28092022
ModmedRejection27092022
ModmedRejection26092022
ModmedRejection25092022
ModmedRejection24092022

I need to keep ModmedRejection28092022,ModmedRejection27092022
and rest should be deleted

@HeartCatcher
Kindly refer this XAML file, it may help you

Forum_Delete_File_DateBased.zip (70.2 KB)
image

I am unable open your file, coming error like unresolved activity

Share some screenshot

What screenshots??

@HeartCatcher
Error screenshots

Please send the logic which you applied in uipath studio, by taking photo

This error I am getting

@HeartCatcher

  1. Use For each folder in folder activity.
  2. mention the folder path which contains ModmedRejection24092022 and more folders
  3. Assign Days=2 , because of filter last 2 days and days should Integer
  4. Use assign and separate the date from the folder by
FolderDateString=System.Text.RegularExpressions.Regex.Match(CurrentFolder.Name,"\d+").ToString.Trim

FolderdateString was the variable type of string
  1. Convert it to date format
FolderDate=DateTime.ParseExact(FolderDateString,"ddMMyyyy",System.Globalization.CultureInfo.InvariantCulture)

where folderDate was variable type of  System.DateTime
  1. Use If condition to filter folder
FolderDate<=Now.Date.AddDays(-BeforeDays)
  1. Then sequence use Delete Folder
CurrentFolder.ToString

image



image

@HeartCatcher
update the pacakage Uipath.system.activities