JFee Posted August 20, 2008 Share Posted August 20, 2008 This is a script I made a while ago when someone was asking about something similar on the forums, so I figured I'd post the code and maybe someone will have a use for it. This goes through a directory of your choosing and deletes all empty directories inside it. It starts at the lowest folder in the hierarchy and works its way out to delete empty folders to ensure folders that only contain other empty folders get deleted as well. Here is the code... only 14 lines #Include <File.au3> $startDir = FileSelectFolder("Choose Directory", @HomeDrive, 2, @ScriptDir) If $startDir Then _delEmpty($startDir) Exit Func _delEmpty($dir) $folderList = _FileListToArray($dir, "*", 2) If @error <> 4 Then For $i = 1 to $folderList[0] _delEmpty($dir & "\" & $folderList[$i]) Next EndIf $fileList = _FileListToArray($dir, -1, 0) If @error = 4 Then DirRemove($dir) EndFunc Hope you enjoy joush 1 Regards,Josh Link to comment Share on other sites More sharing options...
dexto Posted September 3, 2008 Share Posted September 3, 2008 That may do the trick BUT what about empty folders inside of the folder? Link to comment Share on other sites More sharing options...
Mojo Posted September 3, 2008 Share Posted September 3, 2008 Warning! This deletes, on my WinXP SP3 machine, all kinds of folders! Some were a few megabytes in size! However, I changed "DirRemove" to "FileRecycle" to see which folders are deleted and don't think it has anything to do with that. other experiences? You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time. Abraham Lincoln - http://www.ae911truth.org/ - http://www.freedocumentaries.org/ Link to comment Share on other sites More sharing options...
BrettF Posted September 3, 2008 Share Posted September 3, 2008 Looks like some work needs to be done, before I even try it. For expanding it, think of adding a GUI, an example of what will happen (Like outputting the empty folders into a edit box, and logging the folders deleted from an actually delete. Cheers, Brett Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
dexto Posted September 3, 2008 Share Posted September 3, 2008 (edited) ;How about this? ;Not gona delete anything if it contains files ;Not using udfs ;note to self: Oh yeah! I can do it better! $dir = FileSelectFolder("Choose Directory", @HomeDrive, 2, @ScriptDir) If $dir Then emptyfolder($dir) Func emptyfolder($dir) $search = FileFindFirstFile($dir & '\*') If Not ($search = -1) Then While 1 $file = FileFindNextFile($search) If @error Then ExitLoop $mdir = $dir & '\' & $file If StringInStr(FileGetAttrib($mdir), 'D') > 0 Then $dsize = DirGetSize($mdir, 1) If $dsize[1] == 0 Then ConsoleWrite($mdir & @CRLF) DirRemove($mdir, 1) Else emptyfolder($mdir) EndIf EndIf WEnd EndIf FileClose($search) EndFunc ;==>emptyfolder Edited September 3, 2008 by dexto Link to comment Share on other sites More sharing options...
joush Posted April 6, 2021 Share Posted April 6, 2021 I was looking for a simple way to achieve this function (delete all empty folders including in subfolders),and got to this topic. here is my code, based on the original post: #NoTrayIcon #include <File.au3> $start_dir = FileSelectFolder("Choose a Directory", @HomeDrive, 2) If FileExists($start_dir) And StringInStr(FileGetAttrib($start_dir), 'D') Then _delEmpty($start_dir) Func _delEmpty($dir) $folder_list = _FileListToArray($dir, '*', 2) If @error <> 4 Then For $i = 1 To $folder_list[0] _delEmpty($dir & '\' & $folder_list[$i]) Next EndIf FileFindFirstFile($dir & '\*') If @error Then DirRemove($dir) FileClose($dir) EndFunc ;==>empty_dir The reason I chose to use FileFindFirstFile insted of _FileListToArray, is Because it's much faster, especially in folders that contain a lot of files note to dexto; assuming that a folder is empty because its size is 0 bytes is wrong, they are files that are 0 bytes (e.g. simbolic link's and empty files). Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now