How to delete empty folders with StudioX

Does anyone know which activity to use to find out if a folder is empty in order to delete the empty folders?

Thx

Katrijn

Hi @Katrijn

You may use this approach to determine if the folder has any files in it.

  1. Save the out from Get Folder Info using ‘Save for Later Use’
  2. The Files property as shown in the image will give you the number of files inside. If this value is 0 then you can safely delete the folder.
1 Like

Directly us e delete file activity and then give full path of your folder inside delete activity.

Ex: If I want to delete New Folder
image
then path is C:\New folder
image

Activity:
image

I tried this but I can’t find the Files Property. It doesn’t show up.

This would work if I wanted to delete a specific folder. I want to delete all the empty folders so I don’t have a specific path. i need to test if a folder is empty and if it is, delete it. It’s the part about testing if it’s empty that I can’t get to work.

You can check condition using this

strdirPath="D:\Arivu\FolderName"

in If condition check the folder is empty or not.
If(Directory.GetFiles(strdirPath).Length == 0 && Directory.GetDirectories(strdirPath).Length == 0)

True->Folder is empty

@Katrijn
That’s because you’re looking for it in the ‘Get Folder Info’ activity. Since you’re trying to ‘get’ the info first, you need to ‘Save it for Later Use’ (please refer to the steps I mentioned below the image in my reply) The option is visible in your screenshot above the ‘Use Saved Value’ command that you’re trying.
Once you have saved the info, you can access it in any activity you call after this. (I used ‘Message Box’ as an example but you can use it in ‘IF’ activity to directly check if the count is 0 or not)

@Sagar_Gupta1

You can check condition using this
strdirPath="D:\Arivu\FolderName"

The reason for this being tricky is that the OP is trying this in StudioX - not in Studio - as seen from the post title. Using ‘Assign’ activity is not too straight forward in StudioX because one needs to often use ‘Advanced Editor’ and modify the datatype in this format:

From →Saved.Values(Of ExcelValue)("VariableName")
To → Saved.Values(Of <desired datatype>)("VariableName")

@Katrijn
Here is a workflow that does your required task:
FolderCleaner.zip (72.3 KB)

Few key pointers about the logic:

  1. The gist of the logic is just as @Sagar_Gupta1 explained. We will check the number of files and subfolders in the folder in question, and delete if they are both zero (using AND condition, not OR)
  2. You can use ‘Get Folder Info’ activity to get details on the folder, but it gives you a DirectoryInfo object, which is an array of directories. It’s more difficult to deal with subfolders by using the standard ‘Use Saved Value’ feature.
    Instead, for the parent folder, the logic uses a simple GetDirectories method which Sagar has used in his logic.
  3. Iterating over each subfolder to test whether it should be deleted, we check the folder info using ‘Files’ and ‘Folders’ property on output of ‘Get Folder Info’ activity.

You can test this workflow by running directly. The project has a test folder with the following hierarchy.
image
Child 1 - has 1 file
Child 2 - empty folder
Child 3 - has a subfolder

When you run the project, it should delete only the folder Child 2.
I hope this clears a few things for you and that you can use this for your task.

A sidenote, StudioX works really well and makes things easy if you’re using Excel or other office apps.
But if you’re just after file/folder operations, Studio might make it easier for you.
Sagar’s approach is very easy to implement with Studio.

Best regards.

Thank you for your help.

I think I’m trying to do something that is still too difficult for where I’m at in my training :).

I did find the files property now.
Thank you for the workflow. I’ll implement it in the robot I was trying to built.

1 Like

Please feel free to ask if you get stuck at any step.

Happy automation! :slight_smile:

I know this is an old post, but just wanted to say thank you! i was trying to just do an “If” statement where if the folder directory was empty it would delete the folder but this was not working, and i think I understand now why (it would need to be that the name/path of folder is essentially blank, which is not possible for this to work as i originally designed it), but i was able to follow these steps and it worked first try!!

1 Like

Hey @Dave_Conkey
Glad to know this solution helped you after all this time! :slight_smile:

Cheers,
Ash.