Sign in to follow this  
Followers 0
JFee

Delete Empty Folders

5 posts in this topic

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


Regards,Josh

Share this post


Link to post
Share on other sites



That may do the trick BUT what about empty folders inside of the folder? ;)

Share this post


Link to post
Share on other sites

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/

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by dexto

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0