Sign in to follow this  
Followers 0
snowman533

removing empty directories

9 posts in this topic

$ddnumber=0

$dsize=DirGetSize(@scriptdir & "\*")

if $dsize = 0 Then
    While $dsize = 0
        DirRemove($dsize)
        $ddnumber += 1
    WEnd
Else
    Exit
EndIf

If $ddnumber=0 Then
    msgbox(0,"No empty directories", "No empty directories found")
Else
    MsgBox(0,"Results", $ddnumber & " empty directories removed")
EndIf

basically this searches for and removes all empty directories, at the moment i am using @scriptdir to test, i cant get the dang thing to run

what am i missing?

thanks in advance

this will be added to my utilities collection


Intermediate AutoIt/Autohotkey User

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Willcards do not work with DirGetSize, create a sub-dir 'test' in script dir.

$dsize=DirGetSize(@scriptdir & "\*")
ConsoleWrite($dsize & @crlf)
$dsize=DirGetSize(@scriptdir & "\test")
ConsoleWrite($dsize & @crlf)

To catch all dirs you'll have to parse through a filelist, maybe with a loop based on _FileListToArray() and a check like StringInStr(FileGetAttrib($filename),"D") to validate found file is a dir.

Edit: Your while loop hangs the script ^_^.

Edited by KaFu

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

hmm

tried creating a folder called test and coding that in

the script just sits there taking up the CPU

what am i missing from my script? or what is wrong with it?

christ, arrays are a pain to learn but theyl save me one day, i think

Edited by snowman533

Intermediate AutoIt/Autohotkey User

Share this post


Link to post
Share on other sites

#Include <File.au3>
#Include <Array.au3>

$ddnumber=0

$FileList=_FileListToArray(@ScriptDir)
If @Error=1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

for $i = 1 to $FileList[0]
    if StringInStr(FileGetAttrib($FileList[$i]),"D") Then
        if DirGetSize($FileList[$i]) = 0 Then
            if DirRemove($FileList[$i]) then 
                ConsoleWrite("Dir removed: " & $FileList[$i] & @crlf)
                $ddnumber +=1
            Else
                ConsoleWrite("Could not remove (empty?) dir: " & $FileList[$i] & @crlf)
            endif
        Else
            ConsoleWrite("Dir not removed (contains files): " & $FileList[$i] & @crlf)
        endif
    EndIf
Next

If $ddnumber=0 Then
    msgbox(0,"No empty directories", "No empty directories found")
Else
    MsgBox(0,"Results", $ddnumber & " empty directories removed")
EndIf

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

thanks, now to learn about those arrays ^_^

any ways to add all subdirectories and all subdirectories beyond that?

Edited by snowman533

Intermediate AutoIt/Autohotkey User

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

cool, thanks again

one last thing thats been bothering me, i cannot find the answer for this

how can i set up a control that will let you browse for a directory to set as the base directory?

and also, after a test on that script, it only affects the scripts directory and not any of the subdirectories or any of their subs or any of their subs and so on

Edited by snowman533

Intermediate AutoIt/Autohotkey User

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

FileSelectFolder() and search forum for recursive filesearch functions to replace _FileListToArray() ^_^...

Edited by KaFu

Share this post


Link to post
Share on other sites

U looked at an example not fitting your needs, this one fits better:

#1 - Array based from #442099

#include <array.au3>

$ddnumber = 0

$var = FileSelectFolder("Delete Empty directories from:", "", 7)
If @error Then Exit

$FileList = RecursiveFileSearch($var, "", "", 2, True)
If @error = 1 Then
    MsgBox(0, "", "No Files\Folders Found.")
    Exit
EndIf

;_ArrayDisplay($FileList)

For $i = 1 To $FileList[0]
    If StringInStr(FileGetAttrib($FileList[$i]), "D") Then; If parent (empty) dir has already been deleted, FileGetAttrib returns Blank and further analysis is skiped.
        If DirGetSize($FileList[$i]) = 0 Then
            If DirRemove($FileList[$i]) Then
                ConsoleWrite("Dir removed: " & $FileList[$i] & @CRLF)
                $ddnumber += 1
            Else
                ConsoleWrite("Could not remove (empty?) dir: " & $FileList[$i] & @CRLF)
            EndIf
        Else
            ConsoleWrite("Dir not removed (contains files): " & $FileList[$i] & @CRLF)
        EndIf
    EndIf
Next

If $ddnumber = 0 Then
    MsgBox(0, "No empty directories", "No empty directories found")
Else
    MsgBox(0, "Results", $ddnumber & " empty directories removed")
EndIf




#cs ----------------------------------------------------------------------------
    #1 - Array based from http://www.autoitscript.com/forum/index.php?showtopic=58558&view=findpost&p=442099
    
    AutoIt Version: 3.2.10.0
    Author: WeaponX
    Updated: 2/21/08
    Script Function: Recursive file search
    
    2/21/08 - Added pattern for folder matching, flag for return type
    1/24/08 - Recursion is now optional
    
    Parameters:
    
    RFSstartdir: Path to starting folder
    
    RFSFilepattern: RegEx pattern to match
    "\.(mp3)" - Find all mp3 files - case sensitive (by default)
    "(?i)\.(mp3)" - Find all mp3 files - case insensitive
    "(?-i)\.(mp3|txt)" - Find all mp3 and txt files - case sensitive
    
    RFSFolderpattern:
    "(Music|Movies)" - Only match folders named Music or Movies - case sensitive (by default)
    "(?i)(Music|Movies)" - Only match folders named Music or Movies - case insensitive
    "(?!(Music|Movies)\b)\b.+" - Match folders NOT named Music or Movies - case sensitive (by default)
    
    RFSFlag: Specifies what is returned in the array
    0 - Files and folders
    1 - Files only
    2 - Folders only
    
    RFSrecurse: TRUE = Recursive, FALSE = Non-recursive
    
    RFSdepth: Internal use only
    
#ce ----------------------------------------------------------------------------
Func RecursiveFileSearch($RFSstartDir, $RFSFilepattern = ".", $RFSFolderpattern = ".", $RFSFlag = 0, $RFSrecurse = True, $RFSdepth = 0)

;Ensure starting folder has a trailing slash
    If StringRight($RFSstartDir, 1) <> "\" Then $RFSstartDir &= "\"

    If $RFSdepth = 0 Then
    ;Get count of all files in subfolders for initial array definition
        $RFSfilecount = DirGetSize($RFSstartDir, 1)

    ;File count + folder count (will be resized when the function returns)
        Global $RFSarray[$RFSfilecount[1] + $RFSfilecount[2] + 1]
    EndIf

    $RFSsearch = FileFindFirstFile($RFSstartDir & "*.*")
    If @error Then Return

;Search through all files and folders in directory
    While 1
        $RFSnext = FileFindNextFile($RFSsearch)
        If @error Then ExitLoop

    ;If folder and recurse flag is set and regex matches
        If StringInStr(FileGetAttrib($RFSstartDir & $RFSnext), "D") Then

            If $RFSrecurse And StringRegExp($RFSnext, $RFSFolderpattern, 0) Then
                RecursiveFileSearch($RFSstartDir & $RFSnext, $RFSFilepattern, $RFSFolderpattern, $RFSFlag, $RFSrecurse, $RFSdepth + 1)
                If $RFSFlag <> 1 Then
                ;Append folder name to array
                    $RFSarray[$RFSarray[0] + 1] = $RFSstartDir & $RFSnext
                    $RFSarray[0] += 1
                EndIf
            EndIf
        ElseIf StringRegExp($RFSnext, $RFSFilepattern, 0) And $RFSFlag <> 2 Then
        ;Append file name to array
            $RFSarray[$RFSarray[0] + 1] = $RFSstartDir & $RFSnext
            $RFSarray[0] += 1
        EndIf
    WEnd
    FileClose($RFSsearch)

    If $RFSdepth = 0 Then
        ReDim $RFSarray[$RFSarray[0] + 1]
        Return $RFSarray
    EndIf
EndFunc  ;==>RecursiveFileSearch

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