# Visual Basic > Visual Basic 6 and Earlier >  [RESOLVED] Check Folder Empty

## k_zeon

Is there a simple Fast way to check a folder is empty.
I want to delete files and then give the option to Auto Delete folder if not files exist.

Would also need to detect sub folders are empty of not. 

so start at the last folder and work backwards deleting folders if empty or leave them if files or folders exist.

An API or plain VB6 code

tks

----------


## Darkbob

> Is there a simple Fast way to check a folder is empty.
> I want to delete files and then give the option to Auto Delete folder if not files exist.
> 
> Would also need to detect sub folders are empty of not. 
> 
> so start at the last folder and work backwards deleting folders if empty or leave them if files or folders exist.
> 
> An API or plain VB6 code
> 
> tks


I have a similar project.  I have to recurse a directory and add files to a database.  I'm getting lazy.  I didn't even look through my list of handy code.  I just asked ChatGPT and got this. 

Sadly it doesn't work!  Lol... this is an obvious error:

 Open sPath & "" & "*.*" For Input As #f

I've got to get less lazy and search through code examples I guess.



```

RecurseDirectory "C:\MyDirectory"

Sub RecurseDirectory(ByVal sPath As String)
    Dim f As Integer
    Dim sFile As String

    ' Open the directory
    f = FreeFile
    Open sPath & "\" & "*.*" For Input As #f

    ' Read and process each file in the directory
    Do Until EOF(f)
        Input #f, sFile
        If sFile <> "." And sFile <> ".." Then
            If (GetAttr(sPath & "\" & sFile) And vbDirectory) = vbDirectory Then
                ' If the file is a directory, recursively process its contents
                RecurseDirectory sPath & "\" & sFile
            Else
                ' If the file is not a directory, print its full path
                Print sPath & "\" & sFile
            End If
        End If
    Loop

    ' Close the directory and clean up
    Close f
End Sub
```

----------


## k_zeon

> I have a similar project.  I have to recurse a directory and add files to a database.  I'm getting lazy.  I didn't even look through my list of handy code.  I just asked ChatGPT and got this.  It lets you recurse a directory tree.  Should be easy to add a flag for a folder with a file in it or delete files or whatever.
> 
> 
> 
> ```
> 
> RecurseDirectory "C:\MyDirectory"
> 
> Sub RecurseDirectory(ByVal sPath As String)
> ...


tks.

But i am only looking to see if there are any files or folders in the current folder.

i delete the files i want as its a duplicate and then want to check the existance of any file or folder still in the current directory.

ie use this to check for files



```
Public Function FolderIsEmpty(myPath As String) As Boolean
    FolderIsEmpty = CBool(Dir(myPath & "*.*") = "") ' empty of files
End Function
```

how can i mod this function to include folders similar to the files i can already check

tks

----------


## Darkbob

That function should work fine in VB6 just as is to see if a folder is empty.  Not exactly sure what you are looking for.

----------


## baka

I use FindFirstFile API's
its both to search files/folder and it can also be used to count files/folders in a folder.

----------


## k_zeon

> That function should work fine in VB6 just as is to see if a folder is empty.  Not exactly sure what you are looking for.


Hi Darkbob
the function i posted only shows True or False on files.
If i have a folder in the current folder this does not work

FolderIsEmptyofFiles = CBool(Dir(myPath & "\*.*") = "") 

i want to add something like

FolderEmpty = CBool(Dir(myPath & "*.*", vbDirectory) = "")

I dont want to recursively search a folder as I only need to check if its empty or not of Folders or Files.

say i have a Path  C:\temp1\Temp2\Temp3

in here i have  

C:\temp1\Temp2\Temp3\image1.jpg
C:\temp1\Temp2\Temp3\image2.jpg
C:\temp1\Temp2\Temp3\image3.jpg
C:\temp1\Temp2\Temp3\Temp4\Image4.jpg

I want to check folders C:\temp1\Temp2\Temp3  to see if it contains Folders or Files

the result should be Yes it contains either Folders or Files

Now say i delete image 1 to 3 and then i want to check if the folder contains any Folders or Files.

it has no files but contains the folder Temp4

I want the check to return False as it still contains the folder Temp4

I dont really want to be recursive and cound the files or folders, i just need to know if either a single file or Folder exists then
i dont delete the current folder. If the current folder does not contain any Files or folders then i delete the folder

tks

----------


## k_zeon

so this is what i finally came up with. if anyone can do this better
ie an API that can handle unicode, pls post



```
Public Function FolderIsEmpty(myPath As String) As Boolean
    Dim FolderEmpty As Boolean
    Dim FilesEmpty As Boolean
    Dim FolderExists As Boolean

    Dim sFile As String
    If Right$(myPath, Len(myPath) - 1) <> "/" Then myPath = myPath & "\"
    sFile = Dir(myPath & "*.", vbDirectory)
    Do While LenB(sFile) <> 0
        If sFile <> "." And sFile <> ".." Then
            sFile = myPath & "\" & sFile
            If (GetAttr(sFile) And vbDirectory) <> 0 Then
               
                FolderExists = True
                Exit Do
            End If
        End If
        sFile = Dir
    Loop

    FilesEmpty = CBool(Dir(myPath & "*.*") = "")    ' empty of files

    If FolderExists = False And FilesEmpty = True Then
        FolderIsEmpty = True
    Else
        FolderIsEmpty = False
    End If


End Function
```

----------


## VanGoghGaming

You could always use the FileSystemObject since it also supports Unicode:


```
Dim fso As New FileSystemObject, fol As Folder
    If fso.FolderExists(myPath) Then
        Set fol = fso.GetFolder(myPath)
        Debug.Print fol.Files.Count
    End If
```

Set a reference to "Microsoft Scripting Runtime" in your project.

----------


## baka

```
Private Declare Function FindFirstFileW Lib "kernel32" (ByVal lpFileName As Long, ByVal lpFindFileData As Long) As Long
Private Declare Function FindNextFileW Lib "kernel32" (ByVal hFindFile As Long, ByVal lpFindFileData As Long) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Function IsFolderEmpty(ByVal Path As String) As Boolean
    Dim Handle  As Long
    Dim B(999)  As Byte
    Dim Count   As Long
    
    Handle = FindFirstFileW(StrPtr(Path & "\*"), VarPtr(B(0)))
    Do
        Count = Count + 1
    Loop Until FindNextFileW(Handle, VarPtr(B(0))) = 0 Or Count > 2
    FindClose Handle
    If Count = 2 Then IsFolderEmpty = True
End Function
```

usage:
IsFolderEmpty("C:\MyFolder")

this will check the folder for files or folders
all folders contain at least 2 items, called "." and ".." if more it means theres something there.
if u want to get the name u can change the struct.
b(44) is the start of the filename

----------


## wqweto

Btw, this might fail on non-empty root folder of any flash drive.

There are no . and .. in the root folder but if there are exactly 2 files/folders in it procedure above will return True.

----------


## kiymik

I'm adding an invisible 'FileListbox' with pattern *.*, setting its path and checking its listcount. It works considerably faster than file system api.

----------


## baka

wqweto
this was just a fast example, if u want to be careful u can do



```
Function isFolderNotEmpty(ByVal Path As String) As Boolean
    Dim Handle  As Long
    Dim B(999)  As Byte
    
    Handle = FindFirstFileW(StrPtr(Path & "\*"), VarPtr(B(0)))
    If Handle <> -1 Then
        Do
            If B(44) <> 46 Then isFolderNotEmpty= True: Exit Do
        Loop Until FindNextFileW(Handle, VarPtr(B(0))) = 0
        FindClose Handle
    End If
End Function
```

----------


## wqweto

Fails if there are exactly two files in the root folder of a flash drive each filename starting with a dot e.g. *.gitattributes* and *.gitignore* :-))

cheers,
</wqw>

----------


## baka

ah its a windows 10+ thing.
well. if so. it need a bit more checking. 
I have windows 7 and starting with a . is invalid.



```
Function FolderNotEmpty(ByVal Path As String) As Boolean
    Dim Handle  As Long
    Dim B(999)  As Byte
    
    Handle = FindFirstFileW(StrPtr(Path & "\*"), VarPtr(B(0)))
    If Handle = -1 Then Exit Function
    Do
        If Not ((B(44) = 46 And B(46) = 0) Or (B(44) = 46 And B(46) = 46 And B(48) = 0)) Then FolderNotEmpty = True
    Loop Until FindNextFileW(Handle, VarPtr(B(0))) = 0
    FindClose Handle
End Function
```

----------


## wqweto

> I have windows 7 and starting with a . is invalid.


Yes, but the limitation is only in Explorer.

In Command Prompt you can always execute *c:> echo. > .aaa* to create a "dot file" and then dbl-click in Explorer to edit it in Notepad.

cheers,
</wqw>

----------


## baka

ah ok. never tried that. so its a explorer thing. well the new version should work when it will skip . and ..
adding .test in that folder show theres something there.

----------


## k_zeon

tks everyone. Post #14 works well.

----------

