Delete png files within folder

Hi Team,
Using below steps to delete .png files from folders Success/Fail before starting the process.
Problem : Its deleting the Success and Fail folder itself
Directory,getFiles(Path1,“*.png”)
Path1 contains C:\process1\Success
for loop - contains delete activity and passing Path1

@Honda
Hope you are Passing the currentPath variable of the for loop

Hope this helps!

Hi @Honda

The issue happens because you are deleting the folder path (Path1) instead of the files inside it.try this - inside the For Each loop, delete each file, not the folder.
use Directory.GetFiles(Path1, “*.png”) to get file paths, then in the loop delete each file like this: File.Delete(filePath)

If helpful, mark as solution. Happy automation with UiPath

Use the For Each File in Folder and inside it, use the Delete File activity, passing CurrentFile.FullName to the Delete File activity. You can filter the For Each File in Folder on “*.png”

This is where it’s being messed up.

You are passing path1 for deletion that why folder is getting deleted.

You got list of files in path using get directory.

Then When you run the loop, current item refers to the file in the path.

Please pass the currentloop/item variable whatever that you have set for your loop in your workflow.

Then it will only delete the file that the loop is referring at the moment.

@Honda,

Use this LINQ in Invoke Code activity

Directory.GetDirectories("C:\YourFolderPath").ToList().ForEach(Sub(subfolder) Directory.GetFiles(subfolder, "*.png").ToList().ForEach(Sub(file) File.Delete(file)))
1 Like

(From file In Directory.GetFiles(“C:\YourFolderPath”, “*.png”)
Select file).ToList().ForEach(Sub(f) File.Delete(f))

Use this hope this helps

  • Use Directory.GetFiles(folderPath, "*.png") to get an array of file paths.
  • Use a For Each loop to iterate through this array of file paths.
  • Inside the loop, use a Delete File activity (or Delete configured for files) and pass the current file path from the loop variable.

Hello @Honda ,

UiPath provides activities to this required deletion.

1. Use activity named "For Each File in Folder"
2. Provide your folder path
3. Provide filter
4. Use delete file