Does anyone know which activity to use to find out if a folder is empty in order to delete the empty folders?
Thx
Katrijn
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.
Get Folder Info
using ‘Save for Later Use’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.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
then path is C:\New folder
Activity:
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)
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:
You can test this workflow by running directly. The project has a test folder with the following hierarchy.
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.
Please feel free to ask if you get stuck at any step.
Happy automation!
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!!
Hey @Dave_Conkey
Glad to know this solution helped you after all this time!
Cheers,
Ash.