Solved

VB script to delete folder

Posted on 2014-11-25
6
142 Views
Last Modified: 2014-12-01
Hi all I am looking for a VB script that will cycle thru the C:\ drive and find a particular folder, and if it exists, delete it and all it sub-folders and contents. This folder will be in a few places especially in the appdata\roaming and appdata\local of every user that has a profile on the computer. With that being said,  I would like the script to go thru every profile folder and find the folder, delete all its contents. Thanks in advance for any assistance.
0
Comment
Question by:xzay1967
  • 3
  • 2
6 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40464429
Hi,

pls try

Dim fso, bgnFolder, subfolder, certfolder

On Error Resume Next

Set fso = CreateObject("Scripting.FileSystemObject")
bgnFolder = "c:"


For each subfolder in fso.GetFolder(bgnfolder).SubFolders
   certfolder = subfolder & "\appdata\local"
	
   If fso.FolderExists(certfolder) then
	If fso.GetFolder(certfolder).Files.Count <> 0 then
		fso.DeleteFile(certfolder & "\*"), DeleteReadOnly
	End If
   fso.DeleteFolder certfolder
   End If
Next

Open in new window


Regards
0
 

Author Comment

by:xzay1967
ID: 40464896
Hi thanks for the response, but the folders which I am trying to delete is called Mozilla, in some places, and Mozilla Firefox in another. So basically I want the script to look for Mozilla* in c:\program files (x86), C:\Programdata, C:\Users\<username>\appdata\Roaming, and C:\Users\<Username>\local. Since I won't know what users have logged on before, I want the script to search from the root of C for any folder with Mozilla in the name whether it be Mozilla, or Mozilla Firefox. I hope this better explains. Thanks again.
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 40468264
We can really open this up by using WMI, but it will take it some time to complete the query and could possibly led to deletion of an unintended folder.  If you want to look at going down this path, here is some code:

strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'Set colFolders = objWMIService.ExecQuery("Select * from Win32_Directory where Name like '%Mozilla%'")
Set colFolders = objWMIService.ExecQuery("Select * from Win32_Directory where Name like '%Trash%'")

For Each objFolder In colFolders
	' Verify the folder still exists if we are removing folders
	' as the parent folder may have already been deleted.
	If objFSO.FolderExists(objFolder.Name) Then
		msgbox "Deleting " & objFolder.Name
		' Uncomment the next line to actually delete the folder
		' objFolder.Delete
	End If
Next

msgbox "**** Done ****"

Open in new window


We can also use the File System object mentioned by Rgonzo and using RegEx to do our matching as we loop through the specific folders.

Dim objFSO, objFolder, objUserFolder, objRegEx

' Use RegEx for Pattern Matching
' Since RegEx is much faster if we just declare it once, use it here 
' to make it more global
Set objRegEx = new RegExp
With objRegEx
	.Global = True
	.IgnoreCase = True
	.pattern = "Mozilla"
End With

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists("C:\Program Files (x86)") Then	
	Set objFolder = objFSO.GetFolder("C:\Program Files (x86)")
	RemoveMozilla(objFolder)
End If

If objFSO.FolderExists("C:\ProgramData") Then	
	Set objFolder = objFSO.GetFolder("C:\ProgramData")
	RemoveMozilla(objFolder)
End If

If objFSO.FolderExists("C:\Users") Then	
	For Each objFolder in objFSO.GetFolder("C:\Users").SubFolders
		If objFSO.FolderExists(objFolder.Name & "\Appdata\Roaming") Then
			Set objUserFolder = objFSO.GetFolder(objFolder.Name & "\Appdata\Roaming")
			RemoveMozilla(objFolder)
		End If
		If objFSO.FolderExists(objFolder.Name & "\local") Then
			Set objUserFolder = objFSO.GetFolder(objFolder.Name & "\Appdata\Roaming")
			RemoveMozilla(objFolder)
		End If
	Next
End If
							
Sub RemoveMozilla (ParentFolder)
	Dim objSubFolder	
	For each objSubFolder in ParentFolder.SubFolders
		If objRegEx.Test(objSubFolder.Name) then
			msgbox "Would Remove " & objsubfolder.Path
			' Uncomment to actually Delete
			' objSubFolder.Delete True
		End If
	Next
End Sub

Open in new window

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:xzay1967
ID: 40473687
Sorry for the late response, I finally tried the scripts and none of them deleted the Mozilla folders from appdata\roaming or appdata\local. In each script I made sure to uncomment line objSubFolder.Delete True. Thanks again.
0
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 500 total points
ID: 40473831
Sorry,

Just noticed a mistake on the first script I posted.  I still had some test values in the script.  Try this:

strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery("Select * from Win32_Directory where Name like '%Mozilla%'")

For Each objFolder In colFolders
	' Verify the folder still exists if we are removing folders
	' as the parent folder may have already been deleted.
	If objFSO.FolderExists(objFolder.Name) Then
		msgbox "Deleting " & objFolder.Name
		' Uncomment the next line to actually delete the folder
		objFolder.Delete
	End If
Next

msgbox "**** Done ****"

Open in new window


If this still does not work, please let me know if a message box displayed the name of the folder and it just failed to delete it or if the path and folder name did not even show in a message box.  Thanks.
0
 

Author Comment

by:xzay1967
ID: 40473860
Ok this one worked great. Your objfolder.delete was already uncommented, lol. No biggie, it did what it was supposed to do, thanks. I made one change, since I want this to run silently, I removed the options for the message box popup. Thanks for being patient with me on this.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now