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
' ****************************************************************************************************************
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)
Please log in to answer
Posted by:
anonymous_9363
14 years ago
Posted by:
Dinesh.Kashyap
14 years ago
Posted by:
Dinesh.Kashyap
14 years ago
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.