VBScript – How to check if folder is empty

Let's say you want to write a script wich deletes specific folders in a directory structure, but only these folders wich are empty.
This script checks to see if a specific folder is empty and it can easely be integrated in a recursive function wich iterates over several folders.

Dim objFSO, objFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists("C:\FolderName\") Then
Set objFolder = objFSO.GetFolder("C:\FolderName\")

If objFolder.Files.Count = 0 And objFolder.SubFolders.Count = 0 Then
MsgBox "The folder is empty"
Else
MsgBox "The folder contains files and/or subfolders"
End If
End If

Here we use the FileSystem object to 'bind' to a specific folder called FolderName in the root of the C-drive.
We then use this folders files and subfolders properties to dertermine whether the folder is actually empty or contain files or subfolders.

SOURCE

LINK

LANGUAGE
ENGLISH