Computing Staff
  • 1

Delete Multiple Folders With Diffrent Paths Using Vbscript

  • 1

Following are the steps:

Check if exist and delete a folder at c:\test1 if found and continue. If not found continue.
Check if exist and delete a folder at c:\programfiles\test2 if found and continue. If not found continue.
Check if a desktop shortcut and start menu shortcut exist and delete if found. If not exit.

Share

1 Answer

  1. Set fso = CreateObject("Scripting.FileSystemObject")
    'Get paths to the shell's folders
    With CreateObject("WScript.Shell") 
      startMenu = .SpecialFolders("StartMenu")
      allStartMenu = .SpecialFolders("AllUsersStartMenu")
      desktop = .SpecialFolders("Desktop")
      allDesktop = .SpecialFolders("AllUsersDesktop")
    End With
    'WScript.Shell doesn't provide the Program Files directory; asking Explorer
    With CreateObject("Shell.Application") 'Reference: ShlObj.h
      progFiles = .Namespace(&H26;).Self.Path 'CSIDL_PROGRAM_FILES
      On Error Resume Next 'Mapping might not appear before Vista
        progFilesx86 = .Namespace(&H2A;).Self.Path 'CSIDL_PROGRAM_FILESX86
      On Error Goto 0
    End With
    
    Del "c:\test1"
    Del fso.BuildPath(progFiles, "test2")
    If Len(progFilesx86) > 0 Then _
      Del fso.BuildPath(progFilesx86, "test2")
    Del fso.BuildPath(startMenu, "a start menu shortcut")
    Del fso.BuildPath(allStartMenu, "a start menu shortcut")
    Del fso.BuildPath(desktop, "a desktop shortcut")
    Del fso.BuildPath(allDesktop, "a desktop shortcut")
    WScript.Quit
    
    Sub Del(sPath)
      If fso.FolderExists(sPath) Then _
        fso.DeleteFolder sPath, True
      If fso.FileExists(sPath) Then _
        fso.DeleteFile sPath, True
    End Sub

    How To Ask Questions The Smart Way

    • 0