Jump to content

Remove Folder from multiple profiles


Recommended Posts

Posted (edited)

If there is already a topic on this, please let me know. I'm trying to write a batch file that will remove a folder from all local profiles on the PC that have it, not just the %ALLUSERS% profile. We have a program that needed to be installed for each user profile that needed it, so it puts a folder under only their local Start menu > Programs. When the program is uninstalled, it doesn't remove the folder from the Start menu, though it does remove the installed files from the local C: drive, killing the shortcut (which is in its own folder) that is left behind on every profile. I have tried using %USERPROFILE% variable for this, but it only removes it for the currently logged-in user, not all profiles that have it. I tried using "C:\Documents and Settings\*\Start Menu\Programs\..." but that didn't work either. And using a login script isn't really an option since I want to just run it once and have them all removed. I did this with a .lnk (shortcut) by using ' DEL "C:\Documents and Settings\shortcut.lnk" /S ' to search that folder and all subfolders, but the same did not work with 'rd' or 'rmdir' and the folder name. Any ideas?

Thanks!

Edited by shorty610

Posted

Here try this VBS script it should do what you want.

You will have to add thew name of the folder to the script.

Save As SearchUsersFolder.vbs

Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim colItems, objItem, StartFolder, User
User = Act.ExpandEnvironmentStrings("%UserProfile%")
Set Wmi = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = Wmi.ExecQuery("SELECT * FROM Win32_UserAccount",,48)
For Each objItem in colItems
WScript.Echo objItem.FullName
If InStr(User,objItem.FullName) Then
StartFolder = User
Set objFolder = Fso.GetFolder(StartFolder)
ShowSubfolders Fso.GetFolder(StartFolder)
Else
Dim V1, V2, V3
V1 = Split(User,"\")
For Each V2 In V1
V3 = V2
Next
Set objFolder = Fso.GetFolder(StartFolder)
User = Replace(User,V3,objItem.FullName)
StartFolder = User
ShowSubfolders Fso.GetFolder(StartFolder)
End If
Next

Function ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
On Error Resume Next
'-> Place The Folder Name In The TestDelete Spot
If InStr(Subfolder.Path,"TestDelete") Then
Fso.DeleteFolder(Subfolder.Path)
End If
ShowSubFolders Subfolder
Next
End Function

Posted

Thanks, but when I ran the script, it gave me a blank error (Windows Script Host) with no message, just an OK button, followed by another WSH error: "ASP.NET Machine Account", then a runtime error refering to line 22, character 9: Error:Path not found; Code:800A004C. However, it did remove the folder in question, but only from the profile currently logged in. I was hoping to remove the folder from all the profiles that have it. I'm not at all familiar with scripts like vbs, and a little familiar with batch files, so I appreciate all the help I can get.

Posted

The first one works on Vista correct this one I tested on Xp and it worked.

I didnt recieve any errors.

Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim colItems, objItem, StartFolder, User
User = Act.ExpandEnvironmentStrings("%UserProfile%")
Set Wmi = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = Wmi.ExecQuery("SELECT * FROM Win32_UserAccount",,48)
For Each objItem in colItems
If InStr(User,objItem.FullName) Then
StartFolder = User
Set objFolder = Fso.GetFolder(StartFolder)
ShowSubfolders Fso.GetFolder(StartFolder)
Else
Dim V1, V2, V3
V1 = Split(User,"\")
For Each V2 In V1
V3 = V2
Next
On error Resume Next
Set objFolder = Fso.GetFolder(StartFolder)
User = Replace(User,V3,objItem.FullName)
StartFolder = User
ShowSubfolders Fso.GetFolder(StartFolder)
End If
Next
Function ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
On Error Resume Next
'-> Place The Folder Name In The TestDelete Spot
WScript.Echo Subfolder.Path
If InStr(Subfolder.Path,"TestDelete") Then
Fso.DeleteFolder(Subfolder.Path)
End If
ShowSubFolders Subfolder
Next
End Function

Posted

Was experimenting a bit and came up with this batch:

set test=%HOMEDRIVE%\Documents and Settings

for /d %%d in ("%test%\*.*") do (

chdir %%d

rmdir "Start Menu\Programs\TestFolder" /s /q

)

Now it's rough, and checks through every single profile on the computer, so it could be a bit slow. I'll keep looking to see how else I might be able to tweak/improve it, such as searching for only applicable profiles.

Posted

If you gave us the actual folder name I could do a little better with it. There shouldn't really be a reason why you cannot run it under %userprofile% at logon!

Posted (edited)

As I've thought more about it, even though my batch works and could be tweaked, I'm still a little concerned about the time it might take to run through each profile. Running on login might be ideal after all...

Edited by shorty610

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...