Home » KBArticles » SCRIPT: check if folder is empty

SCRIPT: check if folder is empty

Option Explicit
Dim oFS : Set oFS = CreateObject(“Scripting.FileSystemObject”)
Function FolderEmpty(strFolderPathName)
 Dim oFiles, oFile, oFolder, oSubFolders, oSubFolder
 Dim blnFileFound : blnFileFound = False
 Set oFolder = oFS.GetFolder(strFolderPathName)
 Set oFiles = oFolder.Files
 If oFiles.Count > 0 Then
  FolderEmpty = False
  Exit Function
 End If
 Set oSubFolders = oFolder.SubFolders
 For Each oSubFolder In oSubFolders
  If Not FolderEmpty(oSubFolder.Path) Then
   FolderEmpty = False
   Exit Function
  End If
 FolderEmpty = True
End Function
Dim strFolderPathName1 : strFolderPathName1 = “file://server/c$/folder
If FolderEmpty(strFolderPathName1) Then
 MsgBox “the folder ” & strFolderPathName1 & ” is empty”
 MsgBox “the folder ” & strFolderPathName1 & ” is not empty”
End If

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.