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

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

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.
Answered 06/30/2010 by: VBScab
Red Belt

Please log in to comment
0
Thanks VBScab ,

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

Thanks,
Dinesh Kashyap
Answered 07/01/2010 by: Dinesh.Kashyap
Orange Senior Belt

Please log in to comment
0
How can we delete the contents of %temp% through this script ?
Please help.....
Answered 09/02/2010 by: Dinesh.Kashyap
Orange Senior Belt

Please log in to comment
Answer this question or Comment on this question for clarity