View

VBScript : How to check if a 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
	Next
	FolderEmpty = True
End Function

Dim strFolderPathName : strFolderPathName = "D:\Tests\Test"
If FolderEmpty(strFolderPathName) Then
	MsgBox "Le répertoire " & strFolderPathName & " est vide."
Else
	MsgBox "Le répertoire " & strFolderPathName & " contient un ou plusieurs fichiers."
End If

You may also like