user3082545
user3082545

Reputation: 1

To delete certain folders in an directory

I need to know how to delete certain folders on specific directory using VB script. This is my folder structure.

C:/WL

1.Dev
2.Local
3.Src123
4.Src456
5.Src789

I want to delete all the folders except "Dev". could someone help on this.

I used the following Vb code also, but didn't work.

Dim wshell
Dim re : Set re = New RegExp
re.Pattern = "^src*"
re.IgnoreCase = True

With CreateObject("Scripting.FileSystemObject")
  With .GetFolder("C:\WL")
    For Each Folder In .SubFolders
      If Folder.Name <> "dev" Then
        wscript.Echo Folder.Name
        'Wscript.Echo "Should delete the folders"

        set wshShell = WScript.CreateObject("WSCript.shell")

        wshShell.Run  "cmd.exe /D C:\ & RMDIR /S /Q &  Folder.Name"

        If re.Test(Folder.Name) Then Call Folder.Delete(True)
    Next
  End With
End With

Upvotes: 0

Views: 3542

Answers (1)

MC ND
MC ND

Reputation: 70933

1 - Correct the If / End If structure of your code

2 - The cmd command you are running does not work. It should be something as

wshShell.Run "cmd.exe /c rmdir /s /q """ + folder.Path + """"

3 - Why the cmd command ?

For Each f In CreateObject("Scripting.FileSystemObject").GetFolder("c:\WL").SubFolders
    If LCase(f.Name) <> "dev" Then
        f.Delete
    End If
Next

Upvotes: 1

Related Questions