Sign in to follow this  
Followers 0
tazdev

Remove Empty Folders

5 posts in this topic

I was trying to remove some folders that had nothing in it. I came up with the bleow function. What I do is in another script I run a dir c:\*.* /s /b . I check the date of the files listed and if they are too old then I have AutoIT delete them. I came across a problem. What about the folders that are now empty. I don't want them either. I added the bottom function.

$ps is the path to search

$fp is is the full file name and path (c:\abc\123)

$sh is the search results if it is 0 then it found something if -1 then it found nothing

Anyway. You may not care but I was proud that I actually did something that I couldn't find on the forum already done

Func CheckFolderForFiles( $fp )
    $ps = $fp & "\*.*"
    $sh = FileFindFirstFile( $ps )  
    If $sh = -1 Then 
        if FileExists($fp & "\") then DirRemove( $fp )
    else
        FileClose( $sh )
    Endif
EndFunc

Share this post


Link to post
Share on other sites



What happens when you have a file without an extension in a folder? What happens to the folder then?

(That's a hint that your code is flawed unless FileFindFirstFile works differently than I remember).

Share this post


Link to post
Share on other sites

Haven't tried DirGetSize. I will look into that.

Even if a file has no extension it will still show up with *.* as a wildcard therefore the error code will be 0 not -1.

Share this post


Link to post
Share on other sites

LOL DirGetSize would work sooooo much better reduced to only 2 lines of code. HAHAHAHAHA

Thanks.

$size = DirGetSize( $fp )
if $size = 0 then DirRemove( $fp )

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