VB script to delete folder

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.
xzay1967Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
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
xzay1967Author Commented:
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
ltlbearand3Commented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

xzay1967Author Commented:
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
ltlbearand3Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xzay1967Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.