Sign in to follow this  
Followers 0
zFrank

search for empty folders and delete them

14 posts in this topic

can i search for empty folders in my computer and delete them?

i think ChrisL Knows how to do it because once he done something like this, if anyone is able to do it then let me know. any example would be very great.

Thanks a lot in advance for the help!


[font="Georgia"]GSM Expert[/font] but not AutoIt :DProud to be Admin Of : http://www.gsmhosting.net/visit my Forum... http://www.gsmhosting.net/vbb/index.php
$Life = "Happy"
If @Error Then
$Life = "Risk"

Share this post


Link to post
Share on other sites



$folder = @ScriptDir & "\"
$search = FileFindFirstFile ($folder & "*.")

If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    If DirGetSize ($folder & $file & "\") < 1 Then
        MsgBox(4096, "Empty Folder:", $file)
    EndIf
WEnd

; Close the search handle
FileClose($search)

Share this post


Link to post
Share on other sites

@ BrettF

very good, this is what i was trying to do/know. thanks again. can we add a progress bar while searching for empty folders? how?


[font="Georgia"]GSM Expert[/font] but not AutoIt :DProud to be Admin Of : http://www.gsmhosting.net/visit my Forum... http://www.gsmhosting.net/vbb/index.php
$Life = "Happy"
If @Error Then
$Life = "Risk"

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

ok, it is not searching in all folders it only works for the current Directory, i used

$folder = "D:\"

but it only works for the folders which are in only D:\ not in D:\ETC

can you tell me about it?

anyone else?

Edited by zFrank

[font="Georgia"]GSM Expert[/font] but not AutoIt :DProud to be Admin Of : http://www.gsmhosting.net/visit my Forum... http://www.gsmhosting.net/vbb/index.php
$Life = "Happy"
If @Error Then
$Life = "Risk"

Share this post


Link to post
Share on other sites

You may have to do that search yourself.

I haven't tried the command but after looking at the >HELP< file ... it shows that you can use FileGetAttrib on each file that is returned for FileFindNextFile. It is the "D"=Directory attribute. So you can continually look for sub directories to your hearts content it seems. Though, like I said, I haven't tried it.

You will have to work out how to drill down through the directories.

Good luck.


Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools.

Share this post


Link to post
Share on other sites

but at least i need a example because i tried but no success.

I made a script that does something like this.

We have a program that doesn't clean itself up. I wrote the script in autoit so it goes 2 deep. There is a folder with folders underneith it. Any of the directories under that folder that are empty get deleted.

If that'd work for ya I can post the code. I'm not a very good coder though heh

Share this post


Link to post
Share on other sites

Create a function that calls itself. The process would be something like:

Func
    Get list of directories from current directory
    Call this function on each folder inside it
    If the folder is empty, delete it
EndFunc

Can't write the code for you, but that should be a solid start. By doing it this way, it will go through each folder and check, and then delete starting with the folder that is furthest from the start (path-wise). This makes it so it will delete a folder that is full of empty folders.


Regards,Josh

Share this post


Link to post
Share on other sites

Alright, so consider yourself lucky. I almost never write the code for someone and post it, but I got interested by the idea and made my own little script. Very simple, yet effective. Don't even dare saying something like "This is great, but could you make it do this" or I'll be very angry.

#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

Regards,Josh

Share this post


Link to post
Share on other sites

Thats great, but can you make it so it so it makes me coffee in the AM? :P

Alright, so consider yourself lucky. I almost never write the code for someone and post it, but I got interested by the idea and made my own little script. Very simple, yet effective. Don't even dare saying something like "This is great, but could you make it do this" or I'll be very angry.

#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

Share this post


Link to post
Share on other sites

How's this?

#Include <File.au3>
#Include <Coffee.au3>

If @HOUR >= 5 And @HOUR <= 10 Then
     _GetCoffee()
    If @error = 1 Then MsgBox(1, "No beans", "Out of beans!")
EndIf

$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

Regards,Josh

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