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
Actually the actionscript should be the following but I think the other works because its empty
folder delete C:\Test
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
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.
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.
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.
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"
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â
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:
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.