How to Delete all files in a directory and also from Subfolders except one specific Subfolder and also keep the folder structure intact

want to delete all the files inside the tick marked folders which also contains subfolders except the one marked in cross which also contains subfolders

Hi @Debojyoti_chanda
Welcome to the UiPath Community!

Check out this already answered thread:

Essentially, you just need to add one extra step to check whether you want to delete a certain directory and its contents.
Have all the directories you want to avoid in a String[] variable array, and use Contains() method to check and skip unwanted folders.

var directories = System.IO.DirectoryInfo.GetDirectories("your_directory_path");
foreach(var dir in directories)
  if("Your_folder" !=
     Directory.Delete(folderPath, true);

Sorry but let me rephrase my question-
I want to delete all the files(.xlsx)inside the root directory and its subfolders but keep the folder structure intact ( not delete the folders) and not delete anything from the archive folder at all which also contains subfolders.

If you want to keep the folder structure, you need to loop through all the subfolders and delete the files using File.Delete(). Use the following code inside an Invoke Code activity (update the startingFolder to your own path):

Dim startingFolder As String = "C:\Test\StartingFolder"
Dim excludeFolder As String = Path.Combine(startingFolder, "Archive").ToUpper

Dim q As New Queue(Of String)
Dim currentDir As String


While q.Count > 0
	currentDir = q.Dequeue()
	For Each f As String In Directory.GetFiles(currentDir)
		Console.WriteLine("Deleting: " + f)

	For Each d As String In Directory.GetDirectories(currentDir)
		If (Not d.ToUpper.Equals(excludeFolder)) Then
		End If
End While
1 Like

can you give me another code where only the files from the other subfolders will get copied to archive subfolders and not the files inside the archive subfolders. btw the code worked like charm thank you very much.

When copying the files to Archive, do you want copy the folder structure also? Or do you just want all files in the parent folder Archive.

E.g. if we copy “Book1.xlsx” from the folder “Bank Book”, should we put it in “Archive\Book1.xlsx” or in “Archive\Bank Book\Book1.xlsx”

i can easily copy the files(only excel files and not the folder) in bank book and bank statement with timestamp to archive folder using the for each file in folder activity but the problem is it also copies the files in archive folder

If you already have the code to copy files, just add an If-statement to check if the source file is in the Archive folder and if true, just skip copying that file.

1 Like

Hi @Debojyoti_chanda,

Replying here to give people reading this thread another option to achieve this using PowerShell.

  1. Get all items in the root location with .xlsx format here the -Recurse option performs a recursive search
  2. Filter objects where the $_.FullName does not match (does not contain) archive in its path (-Quiet returns a boolean) ($_.FullName here is returns the full path to the file)
  3. Now we loop through each piped object and use the versatile Remove-Item method to delete files.
$FolderRootLocation = ""
Get-ChildItem -Path $FolderRootLocation -Recurse -Include '*.xlsx' |  
Where-Object { ($_.FullName | Select-String -NotMatch "archive" -Quiet) } | 
ForEach-Object{Remove-Item -Path $_.FullName}

If you wish to make this dynamic and integrate it with UiPath the script would be as shown below. If you have doubts on how to integrate it with UiPath, please read through this Tutorial :

    [Parameter(Mandatory=$true)] [string]$TargetFolderLocation,
    [Parameter(Mandatory=$true)] [string]$IgnoreFolderWithString,
    [Parameter(Mandatory=$true)] [string]$FileExtensionType

Get-ChildItem -Path $TargetFolderLocation -Recurse -Include (-join('*.',$FileExtensionType))|  
Where-Object { ($_.FullName | Select-String -NotMatch $IgnoreFolderWithString -Quiet) } | 
ForEach-Object{Remove-Item -Path $_.FullName}
1 Like

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