PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Need a vb script to delete subfolders-VBForums
Results 1 to 3 of 3

Thread: [RESOLVED] Need a vb script to delete subfolders

  1. #1

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Resolved [RESOLVED] Need a vb script to delete subfolders

    I have a directory filled with subdirectories. I need vb script that will go into each subdirectory and delete all files and folders within each subdirectory. For example, take the location

    D:\shared\users\

    Inside this directory are subdirectories like...

    D:\shared\users\user1
    D:\shared\users\user2
    D:\shared\users\user3
    D:\shared\users\user4

    What I want to do is delete everything within folder "user1", folder "user2", etc. without deleting folder "user1", "user2", etc.

    Does anyone know how I can do this?

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,132

    Re: Need a vb script to delete subfolders

    If I understand the request, this should do it.
    Code:
    Option Explicit
    
    Dim fso, ofold, ofld 
    
    	Set fso = CreateObject("Scripting.FileSystemObject")
    	'Set to the correct path
    	Set ofold = fso.GetFolder("D:\shared\users")
    	
    	'Loop through the subfolders in the main folder
    	For each ofld In ofold.Subfolders
    		'Delete the contents of the subfolders
    		DeleteFolderContent ofld
    	Next
    	
    	Set ofold = Nothing
    	Set fso = Nothing
    	
    Private Sub DeleteFolderContent(fold)
    Dim fil, fld
    
    	For Each fil in fold.Files
    		fso.DeleteFile fil, true
    	Next
    	
    	For Each fld in fold.SubFolders
    		fso.DeleteFolder fld, true
    	Next
    
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Need a vb script to delete subfolders

    Thanks... I ended up using this, which might be the same as your code:

    vb Code:
    1. Set oFS = CreateObject("Scripting.FileSystemObject")
    2.  
    3. sFolder = "C:\test"
    4.  
    5. For Each subfolder in oFS.GetFolder(sFolder).Subfolders
    6.     For Each file in oFS.GetFolder(subfolder.Path).Files
    7.         oFS.DeleteFile file.Path, True
    8.     Next
    9.     For Each folder in oFS.GetFolder(subfolder.Path).Subfolders
    10.         oFS.DeleteFolder folder.Path
    11.     Next
    12. Next
    13.  
    14. Set oFS = Nothing
    15.  
    16. Msgbox "Done."

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width