Automating Deletion of .exe Files and Recently Deleted Files in a Folder

We have a scenario involving a “For Each File in Folder” activity where some files have a .exe extension. We want to automatically delete all files with the .exe extension.

Additionally, we need a solution to identify and remove files of any extension that were created or modified within the last 15 minutes.

@sauravx.giri Hi @sauravx.giri

You can handle this inside the For Each File in Folder activity by checking each file one by one.

For deleting only .exe files, you can add an If condition like this:

CurrentFile.Extension.ToLower.Trim = ".exe"

Inside the Then block, use the Delete File activity and pass:

CurrentFile.FullName

For the second requirement, where you want to delete files created or modified in the last 15 minutes, you can use this condition:

CurrentFile.CreationTime >= DateTime.Now.AddMinutes(-15) OrElse CurrentFile.LastWriteTime >= DateTime.Now.AddMinutes(-15)

Then again, use Delete File with:

CurrentFile.FullName

If you want to combine both conditions, you can use:

CurrentFile.Extension.ToLower.Trim = ".exe" OrElse CurrentFile.CreationTime >= DateTime.Now.AddMinutes(-15) OrElse CurrentFile.LastWriteTime >= DateTime.Now.AddMinutes(-15)

One small suggestion: before directly deleting the files, first use a Log Message activity to print CurrentFile.FullName. This will help you verify that the correct files are being picked before deletion.

Thanks.

Dhruba

@sauravx.giri

follow the steps below, by this you are going to identify all the files which are havig the .exe extension or any other extension

  • Take assign activity

  • create a variable with the type array of string

  • pass that variable in Assign activity in To section

  • And in value section pass the below expression, at the place of .exe you can pass any other extensions that you want,
    Directory.GetFiles(“C:\Users\YAregala\Downloads”,“*.exe”)

  • Then after you can delete all the files.

Happy Automation
YK

Hi Dhruba,

I need a small help. In my Downloads folder, we have approximately 300 files with different extensions. Our requirement is to delete only those files that were downloaded within the last 15 minutes, regardless of their extension.

However, as per the current approach, the process is looping through all files and taking around 3 minutes to delete a single file that meets the 15-minute condition.

Could you please suggest a more efficient way to handle this?

Hi @sauravx.giri

For 300 files, it should not take 3 minutes to delete one file. Instead of checking and deleting inside many activities, you can first filter the recent files and then delete only those.

Try using this in an Assign activity:

recentFiles = Directory.GetFiles(downloadFolderPath).
    Select(Function(f) New FileInfo(f)).
    Where(Function(x) x.CreationTime >= DateTime.Now.AddMinutes(-15) OrElse x.LastWriteTime >= DateTime.Now.AddMinutes(-15)).
    ToArray()

Then loop through recentFiles and use Delete File with:

item.FullName

Also import:

System.IO
System.Linq

If it is still slow, the delay may be due to antivirus scanning, locked files, or OneDrive/sync issues in the Downloads folder.

Hi Dhruba
I am facing some issue with assign



image

image
I am also mark variable as recentfiles for system.IO.fileinfo
but its not working , they shown error like

@sauravx.giri Hi @sauravx.giri,

The issue is with the variable type.

Your expression is returning multiple files, so recentFiles should not be FileInfo. It should be either:

System.IO.FileInfo()

or simply use a string array approach, which is easier.

Create recentFiles as:

String()

Then use this Assign:

recentFiles = Directory.GetFiles(downloadFolderPath).
Where(Function(f) File.GetCreationTime(f) >= DateTime.Now.AddMinutes(-15) OrElse File.GetLastWriteTime(f) >= DateTime.Now.AddMinutes(-15)).
ToArray()

After that, use For Each on recentFiles, set the item type as String, and pass item directly to Delete File.

Also make sure System.IO and System.Linq are imported.

@sauravx.giri

You can do this with invoke code activity with in one line.
For that follow the below steps,
Take the **Invoke code **activity
Click on Edit code - and enter the below expression
Directory.GetFiles(downloadFolderPath,“*.xlsx”).Where(Function(f) File.GetCreationTime(f) >= Now.AddMinutes(-15) OrElse File.GetLastWriteTime(f) >= Now.AddMinutes(-15)).ToList.ForEach(Sub(f) File.Delete(f))

in above code downloadFolderPath is a argument that i used, click on edit argument and add this argument and your folder path.

This will delete all of your files that added before 15 minutes. Whichever files having .exe extension.

Hope this helps,

Hi Dhruba, This is the activity which I am using in the code

This is the query which i am implement

this is a variable type
image

This is the error which I am facing during running

@sauravx.giri Hi @sauravx.giri,

The issue is here:

recentFiles.ToString

recentFiles is a String[], so when you use .ToString, it becomes System.String[]. That is why UiPath is trying to find a folder named System.String[].

Since your Assign already returns file paths, you should not use For Each File in Folder here.

Use normal For Each activity instead.

Steps:

  1. Keep recentFiles as String[]

  2. Assign:

recentFiles = Directory.GetFiles(Path.Combine(userProfile, "Downloads")).
Where(Function(f) File.GetCreationTime(f) >= DateTime.Now.AddMinutes(-15) OrElse File.GetLastWriteTime(f) >= DateTime.Now.AddMinutes(-15)).
ToArray()

  1. Use For Each activity:
  • Values: recentFiles

  • TypeArgument: String

  • Item name: filePath

  1. Inside For Each:
  • Log Message:
filePath

  • Delete File:
filePath

For Each File in Folder expects a folder path, but here we already have the filtered file list, so normal For Each is the correct activity.

Hi @sauravx.giri

You can handle both conditions inside the “For Each File in Folder” loop using an If condition. First, check the extension using “CurrentFile.Extension.Equals(”.exe")
and delete the file if true. For the second requirement, compare the file’s Created or LastWriteTime with “Now.AddMinutes(-15)”. If the file was created or modified within the last 15 minutes, delete it. This way you can automatically remove both .exe files and recently created/modified files in the same workflow

@sauravx.giri

Try this approach. this will work faster compared to other processes

Thanks Dhruba its work