/build/static/layout/Breadcrumb_cap_w.png

Search and Remove Folder from users profiles through VB Script

Hi Everyone,

I am looking for VB Script which can remove folder from every user's profile(like %userprofile%\ %appdata%). I tried and found one script which was able to remove the folder what its removing only the folder which is created in user's profile.

Example: Need to remove folder(ABC) from ever user like " %userprofile%\Application Data\ABC"

The script which is mentioned below is removing the folder only when it created in "%userprofile%\ABC"

***********************************************************************************
' Sample VB Script to remove the folder "Hmmm" (plus any contents) located in the profile folder for every user on the
' system.
'
' Alter the constant, FolderToDelete, to meet your own requirements
'
Const FolderToDelete = "\ABC"
Const AllUsers = "\All Users"
Dim strComputer
Dim oWMI, oWSH, oFSO
Dim sAllUsersProfile, oAllUsersFolder, sProfilesRoot, oFolder
Dim colFSOSubFolders, colWMISubFolders, colSubFolders, colSubFolders2
Dim intSize
Dim objFolder, objFolder2, objSubFolder
Dim strFolder, strFolderName
' Instantiate the various objects
strComputer = "."
Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Get the Allusers profile folder path first and from this determine profiles parent folder
'
sAllUsersProfile = oWSH.ExpandEnvironmentStrings("%ALLUSERSPROFILE%")
Set oAllUsersFolder = oFSO.GetFolder(sAllUsersProfile)
sProfilesRoot = oAllUsersFOlder.ParentFolder
' Now enumerate all existing user profile folders
Set oFolder = oFSO.GetFolder(sProfilesRoot)
Set colFSOSubfolders = oFolder.Subfolders
' Now go through each existing user profile folder looking for the designated folder to delete
For Each objSubfolder in colFSOSubfolders
if oFSO.FolderExists(sProfilesRoot & "\" & objSubfolder.Name & FolderToDelete) then
intSize = 0
strFolderName = sProfilesRoot & "\" & objSubfolder.Name & FolderToDelete
Set colSubfolders = oWMI.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
For Each objFolder in colSubfolders
GetSubFolders strFolderName
Next
For i = Ubound(arrFolders) to 0 Step -1
strFolder = arrFolders(i)
strFolder = Replace(strFolder, "\", "\\")
Set colFolders = oWMI.ExecQuery _
("Select * from Win32_Directory where Name = '" & strFolder & "'")
For Each objFolder in colFolders
errResults = objFolder.Delete
Next
Next
end if
Next

' ***************************************************************************************************************
Sub GetSubFolders(strFolderName)
Set colSubfolders2 = oWMI.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
For Each objFolder2 in colSubfolders2
strFolderName = objFolder2.Name
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
GetSubFolders strFolderName
Next
End Sub
' ****************************************************************************************************************

0 Comments   [ + ] Show comments

Answers (3)

Posted by: anonymous_9363 13 years ago
Red Belt
0
Er....it says pretty clearly : Alter the constant, FolderToDelete, to meet your own requirements so I figure Const FolderToDelete = "\Application Data\ABC"ought to do it.
Posted by: Dinesh.Kashyap 13 years ago
Orange Senior Belt
0
Thanks VBScab ,

It worked and save my lot of time to find aternative solution. :)

Thanks,
Dinesh Kashyap
Posted by: Dinesh.Kashyap 13 years ago
Orange Senior Belt
0
How can we delete the contents of %temp% through this script ?
Please help.....
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ