The cleaned up folders are kept in a history file, the most recently used one is at first. When the program starts, it reads this file, and shows the files chosen in the past in a combo box. The most recent one is selected. If the user wants to clean another directory, he can select it form a folder selection dialog box (activated by the Select Dir button or by Alt-S), or he can type-in the name of the folder either in this dialog or directly into the combo box. If he changes his mind, closing the GUI (Esc, clicking on [x], Alt-X or clicking the Exit button) exits the program.
If the user clicks on the OK button (or presses Alt-O) the directory shown in the combo box is cleaned, that is, its empty directories are offered to be removed. But before the work starts, the chosen directory is checked if it exists, and a possible trailing \ is removed. At the end the program terminates.
This version of the DelEmpty function shows the name of the currently processed directory in a TrayTip, so the user knows where the directory traversal is at any moment. When an empty subdirectory is found, the user is asked if he wants it to be deleted. If yes, the empty directory is removed, otherwise it stays as it was before. Some statistics is also collected: the number of subdirectories, the number of removed ones and the number of kept empty subdirectories. At the end this information is shown in a message box.
HistoryFile = %A_ScriptDir%\Del0Dir.hst FileRead history, %HistoryFile% Start: Gui Add, ComboBox, y12 w265 vdir, %history% Gui Add, Button, y+10 w75, &Select Dir Gui Add, Button, x+20 w75, &OK Gui Add, Button, x+20 w75, E&xit Gui Show,,Dir to clean GuiControl Choose, dir, 1 return ButtonOK: Gui Submit StringRight c, dir, 1 ifEqual c,\, StringTrimRight dir, dir, 1 ; remove trailing \ FileGetAttrib Attrib, %dir% If Attrib not contains D { MsgBox Invalid directory Gui Show Return } StringReplace history, history, %dir%|,,All history = %dir%|%history% FileDelete %HistoryFile% FileAppend %history%, %HistoryFile% DelEmpty(dir) TrayTip MsgBox Number of subdirectories = %total%`nDeleted empty directories = %deleted%`nKept empty directories = %kept% ButtonExit: GuiClose: GuiEscape: ExitApp ButtonSelectDir: Gui +OwnDialogs FileSelectFolder d, *C:\, 2, Select a directory to clean IfEqual d,, Return GuiControl Text, dir, %d% Return DelEmpty(dir) { Global total, deleted, kept total += 1 FileGetAttrib Attrib, %dir% FileGetTime ModTime, %dir%, M FileGetTime CreateTime,%dir%, C Loop %dir%\*.*, 2 DelEmpty(A_LoopFileFullPath) TrayTip,,%dir% FileRemoveDir %dir% IfEqual ErrorLevel,1, Return deleted += 1 MsgBox 4,,Remove directory %dir%?`nCreated: %CreateTime%`nModified: %ModTime% IfMsgBox Yes Return kept += 1 deleted -= 1 FileCreateDir %dir% FileSetAttrib +%Attrib%, %dir% FileSetTime %CreateTime%,%dir%, C FileSetTime %ModTime%, %dir%, M }50 lines containing AHK commands are much more than needed for this task. As seen in the other thread, the 7 lines below will do for most of us, the rest of the code just makes the script fancy.
DelEmpty("C:\backup") DelEmpty(dir) { Loop %dir%\*.*, 2 DelEmpty(A_LoopFileFullPath) FileRemoveDir %dir% }