Jump to content

Checking if a folder is empty


 Share

Recommended Posts

  • Developers

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.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

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:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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

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

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
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...