As always, there is no limit of what can be added to a script. Here is a commented version with the option to ignore (keep) directories with any of the specified attribute: R = Read-only, S = System, H = Hidden. The desired keep-attributes can be checked in the main GUI. Also, the cleaning is restarted after an empty subdirectory removal, with asking for another directory name to be cleaned. You have to explicitly exit the script if it was enough.
Code:
HistoryFile = %A_ScriptDir%\Del0Dir.hst ; storage for cleaned directories
FileRead history, %HistoryFile%
Start:
total = 0
deleted= 0 ; reset statistics
kept = 0
Gui Add, ComboBox, x5 y14 w265 vdir, %history%
Gui Add, Button, x5 y+13 w75, &Select Dir
Gui Add, Button, x+20 w75, &OK
Gui Add, Button, x+20 w75, E&xit
Gui Add, GroupBox, x275 y2 w40 h80, Keep
Gui Add, Checkbox, x285 y20 vR, R
Gui Add, Checkbox, x285 y+4 vS, S
Gui Add, Checkbox, x285 y+4 vH, H
Gui Show,,Dir to clean
GuiControl Choose, dir, 1 ; pre-select last used directory
return
ButtonOK: ; run the cleaning
Gui Submit ; assign value to dir
IfEqual R,1, SetEnv R, R
IfEqual S,1, SetEnv S, S
IfEqual H,1, SetEnv H, H
RSH = %R%,%S%,%H% ; keep dir with this attribute
StringReplace RSH, RSH, 0,,All
StringRight c, dir, 1
ifEqual c,\, StringTrimRight dir, dir, 1 ; remove occasional trailing \
FileGetAttrib Attrib, %dir%
If Attrib not contains D ; is dir a directory?
{
MsgBox Invalid directory ; if not: tell user
Gui Show ; show old GUI again
Return
}
StringReplace history,history,%dir%|,,All ; remove repetition
history = %dir%|%history% ; prepend chosen dir
FileDelete %HistoryFile%
FileAppend %history%, %HistoryFile% ; save history
DelEmpty(dir) ; do the work
TrayTip ; remove last traytip
MsgBox, ; show statistics
( LTRIM
Cleaned directory = %dir%
Number of subdirectories = %total%
Deleted empty directories = %deleted%
Kept empty directories = %kept%
)
Gui Destroy ; for a fresh start
GoTo Start ; RESTART for further directories
ButtonExit:
GuiClose: ; EXIT
GuiEscape:
ExitApp
ButtonSelectDir: ; folder select dialog
Gui +OwnDialogs ; must complete before return to main GUI
FileSelectFolder d, *C:\, 2, Select a directory to clean
IfEqual d,, Return ; return at cancelled dialog
GuiControl Text, dir, %d% ; put dir in edit box
Return
DelEmpty(dir) ; the main function
{
Global total, deleted, kept, RSH ; variables for statistics, exclusion
total += 1
FileGetAttrib Attrib, %dir% ; get directory properties
if Attrib contains %RSH%
Return
FileGetTime ModTime, %dir%, M ; time, for re-create
FileGetTime CreateTime,%dir%, C
Loop %dir%\*.*, 2 ; try every child directory
DelEmpty(A_LoopFileFullPath) ; clean recursively
TrayTip,,%dir% ; show processed directory
FileRemoveDir %dir% ; remove dir if empty
IfEqual ErrorLevel,1, Return ; go on if not empty
MsgBox 4,,Remove directory?`n%dir%`nCreated: %CreateTime%`nModified: %ModTime%
IfMsgBox Yes
{ ; it is already removed
deleted += 1 ; increment count
Return
}
FileCreateDir %dir% ; if user keeps dir: re-create
kept += 1 ; count
FileSetAttrib +%Attrib%, %dir%
FileSetTime %CreateTime%,%dir%, C ; re-set properties
FileSetTime %ModTime%, %dir%, M
}
If you like bloating the code further, you could save the keep-attributes in the history file together with the corresponding directory names. We can specify if AND or OR is to be used with the attributes. There could be a function to edit the history (most importantly deleting wrong entries). We could specify creation or access time ranges of subdirectories to keep, or exclude/include subdirectories by their names, and these lists of names could be also stored in the history next to the starting directory cleaned. We could specify if the subdirectories of a kept directory should be excluded from the search or only itself. (This would need some deeper changes in the code. Now, if the keep-attributes require, the complete subdirectory is excluded from the search, but user permission is only asked for, after all the empty sub-subdirectories have been removed already.)