Referring to this question, if the folder exists, how can we check if it’s empty ?
Merge this question with the preceding one and delete it.
You’re confusing people with posting twice within such a short time interval.
Do you mean something like this?
Function BFolderEmpty(sPath As String) As Boolean
BFolderEmpty = False
If Trim(sPath) = "" Then Exit Function
If Right(sPath, 1) = GetPathSeparator() Then sPath = Left(sPath, Len(sPath)-1)
If Dir(sPath,16)="" Then Exit Function
If Dir(sPath + GetPathSeparator() + "*.*") = "" Then Exit Function
BFolderEmpty = True
End Function
Or like this?
Function IsFolderEmpty(sPath As String) As String
If Trim(sPath) = "" Then
IsFolderEmpty = "Wrong Path"
Else
If Right(sPath, 1) = GetPathSeparator() Then sPath = Left(sPath, Len(sPath)-1)
If Dir(sPath,16)<>"" Then
If Dir(sPath + GetPathSeparator() + "*.*") = "" Then
IsFolderEmpty = "Folder Exists But Is Empty"
Else
IsFolderEmpty = "Folder Exists And Has File(s)"
EndIf
Else
IsFolderEmpty = "Folder Not Exists"
EndIf
EndIf
End Function
Dear @JohnSUN,
Thank you so much for both functions, they are all I really want.
Sub CheckEmptyFolder
Dim f1$ : f1 = "file://" & Environ("HOME") & "/" & "locbaf0"' Empty folder.
Dim f2$ : f2 = "file://" & Environ("HOME") & "/" & "locbaf"' Not empty.
MsgBox "Check if folder is empty: " & Chr(13) & Chr(13) & _
"BFolderEmpty(''" & f1 & "'') = " & BFolderEmpty(f1) & Chr(13) & _
"BFolderEmpty(''" & f2 & "'') = " & BFolderEmpty(f2) & Chr(13) & _
Chr(13) & Chr(13) & _
"IsFolderEmpty(''" & f1 & "'') = " & IsFolderEmpty(f1) & Chr(13) & _
"IsFolderEmpty(''" & f2 & "'') = " & IsFolderEmpty(f2) & Chr(13)
End Sub
I was glad to help. Perhaps you change the wildcard “*.*” to a single asterisk, this will allow you to detect files without extensions.