Relevance / action script to delete an empty folder

What kind of relevance would be suitable to identify an empty folder? Is there built-in action script command for that?

You could try something like:

(number of files of it + number of folders of it) of folder "C:\test" = 0

And the action would be

delete C:\test
2 Likes

Actually the actionscript should be the following but I think the other works because its empty :slight_smile:

folder delete C:\Test
4 Likes

Thanks @strawgate and @AlanM, this combination works perfectly!

Here is what I use in my action script as a part of some IBM software uninstall task.

if {(number of files of it + number of folders of it) of folder “C:\IBM” = 0}
folder delete "C:\IBM"
endif

If you’re using this in a Fixlet / Task Relevance (which is constantly evaluated) then be aware it might be a little less efficient if there are a large number of files or folders in the directory. Since you don’t actually care how many there are, you can just check for existence rather than counting them -

q: number of files of folder "c:\windows\temp"
A: 52
T: 2.339 ms

q: exists files of folder "c:\windows\temp"
A: True
T: 0.272 ms

q: (exists files of it or exists folders of it) of folder "c:\windows\temp"
A: True
T: 0.293 ms

Alternatively, if this is just in the Action Script and not in Relevance…well, you can try to remove the directory and simply let it fail if it’s not empty - the “rd” command will abort, while giving a successful return code

C:\temp>rd test
The directory is not empty.

C:\temp>echo %ERRORLEVEL%
0

In ActionScript, that could be

dos rd c:\test

or perhaps

cmd /c rd c:\test
3 Likes

This is an interesting approach. Seems a bit scary to depend on this behavior, but definitely clever.

Thanks @JasonWalker! I didn’t really think of the counting workload but your post made me change the action script to:

if {(not exists files of it or not exists folders of it) of folder “C:\IBM”}
folder delete "C:\IBM"
endif

I am curious, why are you using a folder C:\IBM in the first place?

If you use the __Download when doing bigfix actions, then it gets cleaned up automatically. This works in probably 99% of cases.

If for some reason that doesn’t work, then I would recommend creating a folder under C:\Windows\Temp that you delete at the end of the action or when you are done with it. I would recommend starting the name of the folder with BigFix_ so you know where it came from and that if it sticks around for some reason, it is acceptable to delete. You can even have a fixlet/task that cleans up old folders automatically.

You can even use a parameter called temp or tmp or similar and use that to store the path to the temp folder you are going to create folders within, so that way it can be more easily changed for different OSes. ( typically on *nix you would use /tmp )

In general, creating folders at the root of the drive is not a good idea and should be avoided.

2 Likes

You also want to check for existence of the folder first of you’ll get a singular expression refers to non-existent object error. something like below or use “disjunction of”.

if {exists folder "C:\IBM" whose (not exists files of it and not exists folders of it)}
folder delete "C:\IBM"
endif

If you use __Download be aware that anything within it will try to be deleted on the next action so if you have a running object within it a new action can’t start. Long running tasks might want to be in a different location. One common cause of this are installers which launch sub processes and terminate their original executable so that is one of the reasons we added the override { wait | run } actionscript to actually wait for all the sub processes to finish as well.

Also if you are on UNIX remember that some platforms restrict what can be in /tmp or /var/tmp, specifically items with execution bits on, so remember that.

3 Likes

I use this relevance / action script as a part of some IBM software uninstall task. Some IBM software that we are using, are always installed to folder C:\IBM and the uninstall task doesn’t remove that folder automatically.

@jonbisch I have a relevance to check existing folder in task relevance.

1 Like

Hi Jonbisch

If i want to delete specific files say .txt,.zip if it is present inside the folder then how can i do that

Thanks

You’d create a task to build & execute the OS commands to do that. Is this Windows or Linux? Example for Windows:

Waithidden cmd.exe /c del /q "c:\temp\*.zip"
Waithidden cmd.exe /c del /q "c:\temp\*.txt"
3 Likes

Thank you

The solution worked

As a fun bonus, does anyone know how to remove folder structures that are really deep and/or have files with extremely long names? Examples would include application or database folder structures. Normal ‘delete’ or ‘del /q /s’ either hang, fail, or prompt interactively about long names or deep structure.

The only solution we have found is the old Robocopy tool. Amazingly it solves this problem by mirroring an empty folder to the folder structure you’re trying to delete.

Sample action script here using WebLogic as example:

parameter “dir”=“D:\Middleware\wlserver_10.3\samples”

dos rmdir /S /Q "d:\temp\empty"
dos mkdir "d:\temp\empty"
dos robocopy.exe /E /Mir /R:3 /W:2 /XJ /SL /NP “d:\temp\empty” "{parameter “dir”}"
dos rmdir "{parameter “dir1”}"
dos rmdir “d:\temp\empty”

3 Likes

Maybe my testing structure is not deep enough, but, try this actionscript?

folder delete "d:\temp\empty"
https://developer.bigfix.com/action-script/reference/file/folder-delete.html

Before:

After:

1 Like

The combination of very deep folder structures and/or very long file names have caused issues deleting with all traditional means. That’s why I passed along the robocopy trick from my WebLogic guy. I can go back and verify if he has tried the Bigfix folder delete.

I do agree that under normal circumstances, folder delete can and should be used.

2 Likes