Delete at empty folders


#1

I want to delete the empty folder which is in folder of folder of folder.can u give any solution for that.?


#2

Can’t you just use the Delete activity. Works for files or folders

image


#3

Hi @Navyareddy,

dirPath="D:\Arivazhagan\New Folder"
in If condition check the folder is empty or not.
If(Directory.GetFiles(dirPath).Length == 0 && Directory.GetDirectories(dirPath).Length == 0)

If return true Use Delete activity to delete the folder.

Regards,
Arivu


How to check if the folder is empty or not?
#4

Thank You, But before that i want to get all folders in folders and sub folders also.Actually I have a new file directory ,in that 4 folders ,again each folder having 5 folders ,those are empty so i want to display all those first and then delete those 5 folders…can u help me?


#5

Hi @Navyareddy,

private static void processDirectory(string startLocation)
{
    foreach (var directory in Directory.GetDirectories(startLocation))
    {
        processDirectory(directory);
        if (Directory.GetFiles(directory).Length == 0 && 
            Directory.GetDirectories(directory).Length == 0)
        {
            Directory.Delete(directory, false);
        }
    }
}

Use code like this to delete all empty folder inside sub folders also.

Regards,
Arivu


#6

it shows expression error in if condition.


#7

What error you are getting


#8

HI @Navyareddy,

you can refer the below post.

Regards,
Arivu


#9

Thanks a lot it worked for me… thank you so much for the fast replay.


#10

@arivu96