Deletefolder

Hi friends,

I have a folder by name Uk inside that folder I have folders like 04-Dec-2023
03-Dec-2023
02-Dec-2023
01-Dec-2023
30-Sep-2023
29-Sep-2023
ABC-123
Def-678

I need to delete last 3days folder and the AVC folder and Def folder must not get deleted

Please help me

I have used Directory.GetDirectories(path).where(function(x) new fileinfo(x).lastwritetime.date<today.date.adddays(-3))).to array

But this is deleting all the folders

Hi @hanviprebday ,
Have you tried for each folder in folder, then check name of them ?
or you can call them as list of folder then use for each
regards,

Hi @hanviprebday

  1. Create variables for the current date and the previous two dates.

currentDate = DateTime.Now
dateMinus1 = currentDate.AddDays(-1).ToString(“dd-MMM-yyyy”)
dateMinus2 = currentDate.AddDays(-2).ToString(“dd-MMM-yyyy”)
dateMinus3 = currentDate.AddDays(-3).ToString(“dd-MMM-yyyy”)

  1. Use Directory.GetFiles to get all files from a folder

  2. Use For each for iteration

  3. Use If Activityin that

Condition:

(Path.GetFileName(item).Equals(dateMinus1) Or Path.GetFileName(item).Equals(dateMinus2) Or Path.GetFileName(item).Equals(dateMinus3)) And Not (Path.GetFileName(item).Equals(“ABC-123”) Or Path.GetFileName(item).Equals(“Def-678”))

Then

Delete Activity

Else

Hi @hanviprebday

Directory.GetFiles(“yourFolderPath”).Where(Function(x)CDate(new System.IO.FileInfo(x).CreatedDate)>Now.AddDays(-3) and CDate(new System.IO.FileInfo(x).CreatedDate)<Now).ToArray

Note: It will delete the all the folder created in last 3 days and for the current date folder also.
** You can modify this to your requirement also.

Hope this will help you to understand better.

Cheers!

Hi

  • Add a “Get Folders” activity to your workflow.

  • Set the “Path” property to the path of the “Uk” folder.

  • Add a “For Each” activity and connect the output of the “Get Folders” activity to the “Collection” property.

  • Inside the “For Each” activity, add a “Filter DataTable” activity.

  • Connect the “CurrentRow” property from the “For Each” activity to the “DataTable” property of the “Filter DataTable” activity.

  • Set the “Filter Expression” property to “CreationTime >= '” & DateTime.Now.ToString(“yyyy-MM-dd”) & " - 3" & “'”. This will filter out folders older than three days.

Delete filtered folders:

  • Add a “Delete Folder” activity inside the “Filter DataTable” activity.
  • Connect the “OutDataRow” property from the “Filter DataTable” activity to the “Path” property of the “Delete Folder” activity.

Protect “ABC-123” and “Def-678” folders:

  • Add an “If” activity inside the “For Each” activity.
  • Set the “Condition” property to “CurrentRow.Name = ‘ABC-123’ OR CurrentRow.Name = ‘Def-678’”.
  • If the condition is true, add an “Assign” activity inside the “If” activity.
  • In the “Assign” activity, set a variable to indicate that the current folder is protected.

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