Table of Content

Deleting a folder in vbscript is easy but how to delete number of folders like folder patten test*\ ?

TestFolders

First of all, we need to check any folder existing or not for folder pattern.


Private Function TestFolders(FolderPattern, pFolder)

TestFolders = False

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim strDir As String

strDir = Dir(pFolder & FolderPattern, vbDirectory)
'MsgBox strDir
'MsgBox pFolder & strDir

Do While strDir <> ""

If fso.FolderExists(pFolder & strDir) Then
    'MsgBox "Found folder:" & pFolder & strDir
    TestFolders = True
    Exit Function
End If
strDir = Dir

Loop

Set fso = Nothing
End Function

DeleteFolders

Then we can safely to delete folders.


Private Sub DeleteFolders(FolderPattern, pFolder)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If TestFolders(FolderPattern, pFolder) Then
    fso.DeleteFolder (pFolder & FolderPattern)
Else
    'MsgBox pFolder & FolderPattern & " not existing!"
End If
   
End Sub

test sample

We have sample1\, sample2\ under c:\test\,


Private Sub delfoldertest()
'MsgBox TestFolders("sample*", "c:\test\")
DeleteFolders "sample*", "c:\test\"
End Sub