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!!