Skip to content

Instantly share code, notes, and snippets.

@lcloss
Created March 16, 2019 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcloss/5aab3ea21b7bbd7e00c286ac0ece040f to your computer and use it in GitHub Desktop.
Save lcloss/5aab3ea21b7bbd7e00c286ac0ece040f to your computer and use it in GitHub Desktop.
CScript: List Empty Folders recursivelly
Option Explicit
Dim objFSO
Set objFSO=CreateObject("Scripting.FileSystemObject")
If (WScript.Arguments.Count <> 2) Then
WScript.Echo("Usage: cscript ListEmptyFolders.vbs {path} {list file}")
' If at least 1 argument is passed, list them
If (WScript.Arguments.Count > 0) Then
Dim strArg
Dim i
i = 0
For Each strArg in WScript.Arguments
WScript.Echo("Argument(" & i & ") = " & strArg)
i = i + 1
Next
End If
WScript.Quit(1)
End If
' Set Report File
Dim outFile
Dim objFile
outFile = WScript.Arguments(1)
Set objFile = objFSO.CreateTextFile(outFile,True)
Dim strPath
strPath = WScript.Arguments(0)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim objFolder
Set objFolder = fso.GetFolder(strPath)
DeleteEmptyFolders objFolder
' Close Report File
objFile.Close
Sub DeleteEmptyFolders(folder)
Dim subfolder
On Error Resume Next
' First check for empty subfolders
For Each subfolder in folder.SubFolders
DeleteEmptyFolders subfolder
Next
If Err.Number > 0 Then
WScript.Echo "I dont have permission on " & folder.Name
Err.Clear
End If
' Check if folder is empty
If folder.SubFolders.Count = 0 And folder.Files.Count = 0 Then
' WScript.Echo folder.Path & " is empty"
objFile.Write folder.Path & vbCrLf
' fso.DeleteFolder folder.Path
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment