Jump to content

Delete Empty Folders


JFee
 Share

Recommended Posts

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

Link to comment
Share on other sites

  • 2 weeks later...

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

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

Link to comment
Share on other sites

;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
Link to comment
Share on other sites

  • 12 years later...

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...