• Deleting subfolders by script (Server 2003/XP)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Deleting subfolders by script (Server 2003/XP)

    Author
    Topic
    #413484

    Hi,

    I’m looking for a script, that I can run locally on one of my servers, which will delete a folder and any subfolders contained within it. The top level folder and its subfolders will contain files, and possibly further subfolders. I also require the ability to delete subfolders without knowing their name.

    I have found the following vbs script on the Windows 2000 Technet Scripting Guide, which uses WMI…

    *****************************
    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!” & strComputer & “rootcimv2”)
    Set colFolders = objWMIService.ExecQuery _
    (“SELECT * FROM Win32_Directory WHERE Name = ‘c:test'”)
    For Each objFolder in colFolders
    errResults = objFolder.Delete
    Wscript.Echo errResults
    Next
    *****************************

    This works just fine on the top level folder (C:test) even when it contains files, but if it contains a subfolder the script fails with an error of 14 (folder not empty).

    Can anyone offer a solution, or point me in the right direction? This is such a basic administrative task, but I can find no immediate solution?

    TIA,

    Viewing 3 reply threads
    Author
    Replies
    • #912693

      Try the following :

          Dim fso
          Set fso = CreateObject("Scripting.FileSystemObject")
          fso.DeleteFolder("C:Test")
          Set fso = Nothing

      Andrew C

      • #912703

        Thanks for that, but it was the subfolders under C:test that I was interersted in.

        I’ve just posted a solution that uses the File Scripting Object rather than WMI.

        Cheers,

      • #912704

        Thanks for that, but it was the subfolders under C:test that I was interersted in.

        I’ve just posted a solution that uses the File Scripting Object rather than WMI.

        Cheers,

    • #912694

      Try the following :

          Dim fso
          Set fso = CreateObject("Scripting.FileSystemObject")
          fso.DeleteFolder("C:Test")
          Set fso = Nothing

      Andrew C

    • #912695

      Honestly…

      You change one tiny word in Google and you get a completely different set of hits…including the answer you’ve been looking for days. See this link for a solution.

      Cheers,

    • #912696

      Honestly…

      You change one tiny word in Google and you get a completely different set of hits…including the answer you’ve been looking for days. See this link for a solution.

      Cheers,

    Viewing 3 reply threads
    Reply To: Deleting subfolders by script (Server 2003/XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: