Sign in to follow this  
Followers 0
Pieter

Checking if a folder is empty

7 posts in this topic

How do I check if a folder is empty?

$fldr1 = "F:\MediaSync\test\"

If FileExists( $fldr1 & "*.*") Then
    MsgBox(0,"i","Not empty")
Else
    MsgBox(0,"i","Empty")
EndIf
This doesn't work.


iPod + Online calendars = iPodCALsync

Share this post


Link to post
Share on other sites



How do I check if a folder is empty?

$fldr1 = "F:\MediaSync\test\"

If FileExists( $fldr1 & "*.*") Then
    MsgBox(0,"i","Not empty")
Else
    MsgBox(0,"i","Empty")
EndIf
This doesn't work.
FileFindFirstFile() can do that for you and then test for @error :

Return Value

Success: Returns a search "handle" for use with subsequent FileFindNextFile functions.

Failure: Returns -1 if error occurs. If the Folder is empty the @error is set to 1.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Hi Pieter,

Look at DirGetSize() in the helpfile. I'm sure it may suit your need.

:P

Share this post


Link to post
Share on other sites

Hi Pieter,

Look at DirGetSize() in the helpfile. I'm sure it may suit your need.

:P

You could one or more 0Kb files in the directory.....

:nuke:


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

FileFindFirstFile() can do that for you and then test for @error :

Return Value

Success: Returns a search "handle" for use with subsequent FileFindNextFile functions.

Failure: Returns -1 if error occurs. If the Folder is empty the @error is set to 1.

Like this?
$fldr1 = "F:\MediaSync\emptyfolder\"
$sizefldr1 = DirGetSize($fldr1)

If $sizefldr1 = "0" Then
    MsgBox(0,"i","Not empty, " & $sizefldr1)
Else
    MsgBox(0,"i","Empty")
EndIf

iPod + Online calendars = iPodCALsync

Share this post


Link to post
Share on other sites

You could one or more 0Kb files in the directory.....

:P

If you use the extended mode then the array returned from this function is a single dimension array containing the following elements:

$array[0] = Size

$array[1] = Files count

$array[2] = Dirs Count

It could be preference to what is best for the task. No doubt, JdeB, that your reply is just as useful. :nuke:

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Like this?

More like this

$fldr1 = "F:\MediaSync\emptyfolder\"
$sizefldr1 = DirGetSize($fldr1, 1)
If Not @error Then
    If Not $sizefldr1[1] And Not $sizefldr1[2] Then
        MsgBox(0,"i","Empty")
    Else
        MsgBox(0, "i", "Not empty: " & $fldr1)
    EndIf
Else
    MsgBox(0, "i", "Does not exist: " & $fldr1)
EndIf

Edit:

Changed variable in MsgBoxes

Edited by MHz

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