VBScript to delete a folder from user %APPDATA%
I have been searching high and low in google for some ready vbscript to delete a folder from the user %appdata%
When you search microsoft site or most site will return you with this code shown below.The problem with the ussual code below is you need to hard code the location C:\My Documents\Username\Application Data\foldername in your script and if you deploy this via GPO the username will be different for each user!! :-
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = 'C:\\My Documents\\Username\\Application Data\\foldername'")
For Each objFolder in colFolders
errResults = objFolder.Delete
Next
The worst part is the script above does not allow you to put in the variable
%APPDATA%
So i was not satisfied..i was thinking what the f*rk Vbscript is suppose to help us.not make our life difficult.. so i rewrite it..
Dim objFSO, copyFile, vSystemDrive
Set WshShell = WScript.CreateObject("Wscript.Shell")
vAPPDATA = WshShell.ExpandEnvironmentStrings("%APPDATA%")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder(vAPPDATA & "\FolderName")
Yes you need to use the code below in order to use the variable %APPDATA%
vAPPDATA = WshShell.ExpandEnvironmentStrings("%APPDATA%")
or another alternate way of doing it is to grab the explorer.exe process to look for the username
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'explorer.exe'")
For Each objProcess in colProcessList
colProperties = objProcess.GetOwner(strUserName)
Next
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
filesys.DeleteFolder("C:\My documents\" & strUserName & "\Application Data\FolderName")
So is really up to you to choose your way of scripting..the worst is i come across forum which write 100 over line of codes to do the similar task..their boss should just fire them.!!!
When you search microsoft site or most site will return you with this code shown below.The problem with the ussual code below is you need to hard code the location C:\My Documents\Username\Application Data\foldername in your script and if you deploy this via GPO the username will be different for each user!! :-
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = 'C:\\My Documents\\Username\\Application Data\\foldername'")
For Each objFolder in colFolders
errResults = objFolder.Delete
Next
The worst part is the script above does not allow you to put in the variable
%APPDATA%
So i was not satisfied..i was thinking what the f*rk Vbscript is suppose to help us.not make our life difficult.. so i rewrite it..
Dim objFSO, copyFile, vSystemDrive
Set WshShell = WScript.CreateObject("Wscript.Shell")
vAPPDATA = WshShell.ExpandEnvironmentStrings("%APPDATA%")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder(vAPPDATA & "\FolderName")
Yes you need to use the code below in order to use the variable %APPDATA%
vAPPDATA = WshShell.ExpandEnvironmentStrings("%APPDATA%")
or another alternate way of doing it is to grab the explorer.exe process to look for the username
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'explorer.exe'")
For Each objProcess in colProcessList
colProperties = objProcess.GetOwner(strUserName)
Next
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
filesys.DeleteFolder("C:\My documents\" & strUserName & "\Application Data\FolderName")
So is really up to you to choose your way of scripting..the worst is i come across forum which write 100 over line of codes to do the similar task..their boss should just fire them.!!!
Comments
Thanks a lot for posting this information. Last week I created a script which renames Microsoft Outlook signatures stored in the %UserProfile%.
Without this I would not be able to quickly find out or use another method to retrieve the variable.
Ivan Versluis
http://www.networknet.nl
I love this, but I am stuck, I am not good at scripting but I keep getting an error. I want to delete the folder cache which has this path.
application data\sun\java\deployment\cache
I get an error that the folder doesn't exist?
Run it from a command line to test it.
for /d %d in ("c:\documents and settings\*") do rd /s/q "%d\Application Data\Sun\Java\Deployment\cache"