Created
March 16, 2019 19:59
-
-
Save lcloss/1c3a8c49a1eca25a454070f0df8d1d62 to your computer and use it in GitHub Desktop.
CScript: Delete Empty Folders recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
' From: https://blogs.msdn.microsoft.com/jjameson/2009/11/03/deleting-empty-folders/ | |
If (WScript.Arguments.Count <> 1) Then | |
WScript.Echo("Usage: cscript DeleteEmptyFolders.vbs {path}") | |
' 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 | |
Dim strPath | |
strPath = WScript.Arguments(0) | |
Dim fso | |
Set fso = CreateObject("Scripting.FileSystemObject") | |
Dim objFolder | |
Set objFolder = fso.GetFolder(strPath) | |
DeleteEmptyFolders objFolder | |
Sub DeleteEmptyFolders(folder) | |
Dim subfolder | |
' First check for empty subfolders | |
For Each subfolder in folder.SubFolders | |
DeleteEmptyFolders subfolder | |
Next | |
' Check if folder is empty | |
If folder.SubFolders.Count = 0 And folder.Files.Count = 0 Then | |
WScript.Echo folder.Path & " is empty" | |
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