Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deleting subfolders by script (Server 2003/XP)

    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,

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Deleting subfolders by script (Server 2003/XP)

    Try the following :<pre> Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.DeleteFolder("C:Test")
    Set fso = Nothing</pre>

    Andrew C

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting subfolders by script (Server 2003/XP)

    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,

  4. #4
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting subfolders by script (Server 2003/XP)

    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,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •