How to Delete the folder which has yesterday's date in lt

I want to delete all the folders which has yesterday’s date on it
FOR example:
NewMicrosoftExcel- 10-09-23

i want to delete these files
How do i do it

Hi @Girid

In UiPath, you can delete folders that match a specific date by following these steps:

  1. Get a list of all folders in the directory where you want to search for folders with yesterday’s date.
  2. Iterate through the list of folders.
  3. For each folder, extract the date from its name.
  4. Compare the extracted date with yesterday’s date.
  5. If the extracted date matches yesterday’s date, delete the folder.

Here’s a high-level workflow for achieving this:

1. Use the "Assign" activity to calculate yesterday's date. You can do this in a script using VB.NET or C# and assign the result to a variable.

2. Use the "Assign" activity to specify the directory where you want to search for folders.

3. Use the "Directory.GetDirectories" method to get a list of all folders in the specified directory. Store this list in an array variable.

4. Use a "For Each" loop to iterate through each folder in the array.

5. Inside the loop, use string manipulation functions to extract the date from the folder's name.

6. Compare the extracted date with yesterday's date (you calculated this in step 1).

7. If the dates match, use the "Directory.Delete" activity to delete the folder.

8. End the loop.

9. Your automation is complete.

1 Like

@Nitya1 , thanks for replying but directory.getdirectories is written to loop?

Hi @Girid

No it will not come under loop, define the variable of type array string

Hey @Girid ,

You can use a for each file in folder and give the path from where you have to delete
and inside the for each file in folder loop , drag and drop an if activity and check if the files contain yesterday’s date.
If true then delete

Hope it helps you !

1 Like

Can you send me an example code please
I am not understanding

How to check in if activity?

Hey @Girid ,
you can write
CurrentFile.Name.Contains(Now.AddDays(-1).tostring("MM-dd-yy")) in if activity
This basically will check if file has yesterdays date in it

1 Like

Can you send me an example code please

@Girid , Please refer below file (8.5 KB)

Hope it helps you!

1 Like

Thanks @Vikas_M
Kindly explain me the if code proprly please

  1. “CurrentFile” is like a placeholder or a variable that represents each file as you go through them one by one. Think of it as a way to point to the file currently being processed.

  2. CurrentFile.Name: This part refers to the name of the file that is currently being looked at.

  3. .Contains(…): This is a check or a test that you’re performing on the file name.

  4. Now.AddDays(-1): “Now” is the current date and time. “.AddDays(-1)” means you’re going back in time by one day, so it’s like finding the date for yesterday.

  5. .ToString(“MM-dd-yy”): This takes the date from step 5 (yesterday) and converts it into a specific format: month-day-year, like “09-09-23”.
    Hope it helps you out!

1 Like

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