VBScript to search and delete folder
Hi, I have a folder (say "Humm") that may be under any user in c:\Documents and Settings. I am looking for a script that searches for this folder and then deletes it.
I have searched the web but can't find one. Any ideas?
Thanks,
Mike.
I have searched the web but can't find one. Any ideas?
Thanks,
Mike.
0 Comments
[ + ] Show comments
Answers (37)
Please log in to answer
Posted by:
spartacus
18 years ago
OK, see if this works for you - it's an adaptation of the code found in this article
Regards,
Spartacus
' 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 = "\Hmmm"
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
' ****************************************************************************************************************
Regards,
Spartacus
Posted by:
jimmy.alappatt
18 years ago
the script must be kept after installfinalize and condition must be REMOVE
it will forcefully remove the folder, so all the files and the folders will be removed
'==========================================================================
Option Explicit
On Error Resume Next
'==========================================================================
'
' DESCRIPTION: To append the environment variable.
'
' NAME: delete_folder.vbs
'
'
' AUTHOR: Jimmy Antony
' DATE: 01/10/2006
' USAGE: To delete the folder "Humm" which is present any where in C:\Documents and Settings
'
' SEQUENCE: After installfinalize
'
'
' NOTE: This script will remove the folder forcefully
'
'==========================================================================
ShowFolderList("C:\Documents and Settings")
Function ShowFolderList(folderspec)
Dim fso, f, f1, s, sf
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set sf = f.SubFolders
For Each f1 in sf
ShowFolderList(f1.path)
if strcomp(f1.name,"Humm") = 0 then
fso.deletefolder f1.path, true
Exit for
Exit function
End if
Next
End Function
'==========================================================================
it will forcefully remove the folder, so all the files and the folders will be removed
'==========================================================================
Option Explicit
On Error Resume Next
'==========================================================================
'
' DESCRIPTION: To append the environment variable.
'
' NAME: delete_folder.vbs
'
'
' AUTHOR: Jimmy Antony
' DATE: 01/10/2006
' USAGE: To delete the folder "Humm" which is present any where in C:\Documents and Settings
'
' SEQUENCE: After installfinalize
'
'
' NOTE: This script will remove the folder forcefully
'
'==========================================================================
ShowFolderList("C:\Documents and Settings")
Function ShowFolderList(folderspec)
Dim fso, f, f1, s, sf
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set sf = f.SubFolders
For Each f1 in sf
ShowFolderList(f1.path)
if strcomp(f1.name,"Humm") = 0 then
fso.deletefolder f1.path, true
Exit for
Exit function
End if
Next
End Function
'==========================================================================
Posted by:
Meic
18 years ago
Posted by:
Meic
18 years ago
Hi Spartacus,
Your script works great, bt maybr I didn't explain things clearly enough.
As I don't know whose profile the "Humm" folder may be in - I need it to search every users profile in Docs & Settings.i.e. I need it to search every sub folder of Docs and Settings. I have tried changing:
Const AllUsers = "\All Users"
to something like:
Const AllUsers ="\Documents and Settings"
but it doesn't delete the folder. Any ideas wht else I should change?
Thanks,
Mike.
Your script works great, bt maybr I didn't explain things clearly enough.
As I don't know whose profile the "Humm" folder may be in - I need it to search every users profile in Docs & Settings.i.e. I need it to search every sub folder of Docs and Settings. I have tried changing:
Const AllUsers = "\All Users"
to something like:
Const AllUsers ="\Documents and Settings"
but it doesn't delete the folder. Any ideas wht else I should change?
Thanks,
Mike.
Posted by:
Miro
18 years ago
Hi, this one removes all patch uninstall directory from Windows Folder.
As i readed in the Forums the folder "$hf_mig$" should not be deleted.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set OFWindows = oFSO.GetFolder("c:\windows")
Set OWinFolders = OFWindows.SubFolders
For Each OWinFolder in OWinFolders
if Ucase(left(OWinFolder.name , 14)) = "$NTUNINSTALLKB" then OWinFolder.delete force
Next
Miro
As i readed in the Forums the folder "$hf_mig$" should not be deleted.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set OFWindows = oFSO.GetFolder("c:\windows")
Set OWinFolders = OFWindows.SubFolders
For Each OWinFolder in OWinFolders
if Ucase(left(OWinFolder.name , 14)) = "$NTUNINSTALLKB" then OWinFolder.delete force
Next
Miro
Posted by:
taz081175
16 years ago
Posted by:
anonymous_9363
16 years ago
Posted by:
taz081175
16 years ago
Posted by:
captain_planet
16 years ago
VBScab means something like this (test it first):
' Sample VB Script to remove the folder "Hmmm" (plus any contents) located in the profile folder for every user on the
' system.
'
'define your folders in an array
Dim folders(3)
folders(0) = "folder1"
folders(1) = "folder2"
folders(2) = "folder3"
folders(3) = "folder4"
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 foldername In folders 'loop through your folder names
For Each objSubfolder in colFSOSubFolders
If oFSO.FolderExists(sProfilesRoot & "\" & objSubfolder.Name & "\" & foldername) then
intSize = 0
strFolderName = sProfilesRoot & "\" & objSubfolder.Name & "\" & foldername
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
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
' ****************************************************************************************************************
Posted by:
roma70
15 years ago
Posted by:
anonymous_9363
15 years ago
Hi guys, what about if you need to delete a folder in the root of C:\ Example C:\roma70\test\Test.iniTry this. It's particularly brutal, in that it has the 'Force' flag for the folder deletion, i.e. it will delete it, even if the folder has content. There's no reason why you couldn't add in code to check for file content, though.
I would like to be able to remove files and folders.
Option Explicit
Dim strFolderTree
Dim objFSO
Dim blnResult
strFolderTree = "C:\Level1\Level2\Level3\Level4"
Set objFSO = CreateObject("Scripting.FileSystemObject")
blnResult = CreateFolderTree(strFolderTree)
blnResult = DeleteFolderTree("C:\Level1")
Set objFSO = Nothing
Function CreateFolderTree(ByVal strFolderPath)
CreateFolderTree = False
With objFSO
If Not .FolderExists(strFolderPath) Then
If CreateFolderTree(.GetParentFolderName(strFolderPath)) Then
CreateFolderTree = True
Call .CreateFolder(strFolderPath)
End If
Else
CreateFolderTree = True
End If
End With
End Function
Function DeleteFolderTree(ByVal strPath)
Dim objFolder
With objFSO
'// Enumerate the sub-folders in the folder we've been given.
For Each objFolder In .GetFolder(strPath).SubFolders
DeleteFolderTree(objFolder.Path)
Next
'// Now that the folder we've been given is empty, we can delete it
.DeleteFolder(strPath), True
End With
End Function
Also do you know any Vbs book for newby?Books, schmooks...the best way to learn is to d/l scripts and use them, then edit to suit your requirements. There's a handy reference here http://www.devguru.com/technologies/vbscript/home.asp, LOADS of good (and plenty of bad!) examples here http://cwashington.netreach.net and also at
http://www.cruto.com/resources/vbscript/vbscript-examples/vbscript-sitemap.asp
http://www.computerperformance.co.uk/vbscript/index.htm
http://www.thescriptlibrary.com/Default.asp?Action=SubIndex&ScriptLanguage=VBScript
Posted by:
moinkm5
15 years ago
Posted by:
roma70
15 years ago
Posted by:
aogilmor
15 years ago
ORIGINAL: roma70
Thanks guys, I did find something that suit my need on the Scripting Guy, thanks for you time I will keep those scripts in mind for future need
Glad you did! I've found them to be a great resource and it keeps me from bugging folks here too much :-) seriously, give props (ratings) to those who helped you.
Posted by:
u_larin812731
14 years ago
Hi Spartacus, I'm kind of having the same issue as Meic.
I'm trying to remove Microsoft Office Shortcuts from all users profiles.
how can i modify the script that you posted to search for shortcuts\file instead of a folder?
for example
Microsoft Office excel 2003
Microsoft Office Word 2003
Any help will be appreciated.
I'm trying to remove Microsoft Office Shortcuts from all users profiles.
how can i modify the script that you posted to search for shortcuts\file instead of a folder?
for example
Microsoft Office excel 2003
Microsoft Office Word 2003
Any help will be appreciated.
Posted by:
aogilmor
14 years ago
here is something i used to delete outlook shortcuts. modify it for whatever yours are named. and gimme some rating points if it works for ya! :-)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile where Name = 'Microsoft Office Outlook 2003' AND Extension = 'lnk'")
For Each objFile in colFiles
objFile.Delete
Next
Posted by:
aogilmor
14 years ago
Posted by:
anonymous_9363
14 years ago
Posted by:
Jsaylor
14 years ago
Funny how that works, isn't it? I've gotten points for some really weird posts I didn't think were all that helpful, then nothing when I go way out of my way to write up some incredibly detailed analysis.
Fortunately the rating system is more or less meaningless here, so I don't worry about it too much.
Fortunately the rating system is more or less meaningless here, so I don't worry about it too much.
Posted by:
u_larin812731
14 years ago
Sorry for the delay guys but, I had an emergency and had to go for two weeks.
Unfortunately your script did not work but, I think I know why Im having this issue.
There is only one user (test) account that leaves left overs and I'm trying to figure out why.
If I log on as another user all shorcuts get deleted, most likely is a problem with this account
I appreciate all your help though.
Unfortunately your script did not work but, I think I know why Im having this issue.
There is only one user (test) account that leaves left overs and I'm trying to figure out why.
If I log on as another user all shorcuts get deleted, most likely is a problem with this account
I appreciate all your help though.
Posted by:
anonymous_9363
14 years ago
Posted by:
NewtotheVB
14 years ago
Posted by:
anonymous_9363
14 years ago
There must be a quadzillion examples of scripts like this around. Start here at AppDeploy and search for 'www.computerperformance'. That will turn up a heap of posts with other links to VBScript sites, one of which will have something similar to what you want.
Some words of advice:
- avoid hard-coding stuff like 'C:\Documents and Settings'. That restricts the script's "ageing" capability (meaning that it will fail on Vista/Windows 7, for example). Instead, get as far down the path as you can using data which is available to you. In this case, that will be the 'Application Data' folder which can be divined from an environment variable.
- error-trap EVERYTHING. Assume nothing will work, including declaration of objects, even ones "which will always work" like FileSystemObject.
Some words of advice:
- avoid hard-coding stuff like 'C:\Documents and Settings'. That restricts the script's "ageing" capability (meaning that it will fail on Vista/Windows 7, for example). Instead, get as far down the path as you can using data which is available to you. In this case, that will be the 'Application Data' folder which can be divined from an environment variable.
- error-trap EVERYTHING. Assume nothing will work, including declaration of objects, even ones "which will always work" like FileSystemObject.
Posted by:
aogilmor
14 years ago
Posted by:
QueBusQuoi
14 years ago
Hi, I'm trying to take spartacus' example but apply it to a Vista / 7 environment.
In my case, I'm looking at deleting a folder under C:\Users\{username}\AppData\Roaming
I tried replacing %ALLUSERSPROFILE% with %APPDATA%...
I'm also unsure what to do with the constant AllUsers...
Any help would be much appreciated!
In my case, I'm looking at deleting a folder under C:\Users\{username}\AppData\Roaming
I tried replacing %ALLUSERSPROFILE% with %APPDATA%...
I'm also unsure what to do with the constant AllUsers...
Any help would be much appreciated!
Posted by:
honestgoofy
14 years ago
So I hope you guys are still reading these posts. I am trying to do the above but I need a single script regardless if it is XP or Win7. How would I modify this script to search for the folder? For instance on Windows 7 the location is:
c:\users\%username%\Appdata\locallow\sun\java\deployment\cache\6.0
but for XP is is:
c:\documents and settings\%username%\Application data\sun\java\deployment\cache\6.0
My guys want to push this via SCCM.
c:\users\%username%\Appdata\locallow\sun\java\deployment\cache\6.0
but for XP is is:
c:\documents and settings\%username%\Application data\sun\java\deployment\cache\6.0
My guys want to push this via SCCM.
Posted by:
honestgoofy
14 years ago
Posted by:
honestgoofy
13 years ago
ok here th code I am using. This will determins the Server OS and then run a sub routine for each OS. I am trying to publish this via SCCM and it seems to be breaking on both XP and Win7. The code is blue works but the red is not. I am still pretty new at scripting so I need some help either inserting error checking or if anyone know what IO need to do to get it to work.
On a side note if I run this outside of SCCM (on the local machines) it works.
-----------------------------------------------------code----------------------------------------------------------------------------------
Dim strComputer, VerOS, VerBig, Ver9x, Version9x, OS, OSystem
Dim objWMI, objItem, colItems
Dim oWMI, oWSH, oFSO
Dim sAllUsersProfile, oAllUsersFolder, sProfilesRoot, oFolder
Dim colFSOSubFolders, colWMISubFolders, colSubFolders, colSubFolders2
Dim intSize
Dim objFolder, objFolder2, objSubFolder
Dim strFolder, strFolderName
Dim objFile
Dim FolderToDelete
Dim AllUsers
Dim strCommandWin7, strCommandXP
On Error Resume Next ' this is set because the designated file may be locked from deletion
'Determin OS
strComputer = "."
' This is where WMI interrogates the operating system
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
' Here we filter Version
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
' Spot VerBig variable in previous section
' Note the output variable is called OSystem
Select Case VerBig
Case "6.1" OSystem = "Windows 7"
Case "6.0" OSystem = "Vista"
Case "5.2" OSystem = "Windows 2003"
Case "5.1" OSystem = "XP"
Case "5.0" OSystem = "W2K"
Case "4.0" OSystem = "NT 4.0**"
Case Else OSystem = "Unknown - probably Win 9x"
End Select
' This will display the complete Operating System version
'Wscript.Echo "Version No : " & VerBig & vbCr & "OS System : " & OSystem
' Instantiate the various objects
strComputer = "."
Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
If OSystem = "Windows 7" Then
Call Win7
Else
Call XP
End If
' ***************************************************************************************************************
Sub Win7
'Search and destroy any file with @AIS in the name and send to c:\temp
'WScript.Echo ("Search and destroy any file with @AIS in the name and send to c:\temp")
strCommandWin7 = "cmd /c del c:\users\*@*ais*.txt /s > c:\temp\cookiedeleteWIN7.txt"
' This will display the command that will be run in a command promt
'WScript.Echo (strCommandWin7)
Set StdOut = WScript.StdOut
Set objShellWin7 = CreateObject("WScript.Shell")
set objScriptExecWin7 = objShellWin7.Exec (strCommandWin7)
' Get the Allusers profile folder path first and from this determine profiles parent folder
sAllUsersProfile = oWSH.ExpandEnvironmentStrings("%USERPROFILE%")
Set oAllUsersFolder = oFSO.GetFolder(sAllUsersProfile)
sProfilesRoot = oAllUsersFolder.ParentFolder
FolderToDelete = "\appdata\locallow\sun\java\deployment\cache\6.0"
AllUsers = "\%userprofile%"
' This will display the path of the users profiles
'Wscript.Echo sprofilesroot
' Now enumerate all existing user profile folders
Set oFolder = oFSO.GetFolder(sProfilesRoot)
Set colFSOSubfolders = oFolder.SubFolders
End Sub
' ***************************************************************************************************************
Sub XP
'Search and destroy any file with @AIS in the name and send to c:\temp
'WScript.Echo ("Search and destroy any file with @AIS in the name and send to c:\temp")
strCommandXP = "cmd /c del C:\Docume~1\*@*ais* /s > c:\temp\cookiedirXP.txt"
' This will display the command that will be run in a command promt
'WScript.Echo strCommandXP
Set StdOut = WScript.StdOut
Set objShellXP = CreateObject("WScript.Shell")
set objScriptExecXP = objShellXP.Exec (strCommandXP)
' 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
FolderToDelete = "\Application Data\Sun\Java\Deployment\cache\6.0"
AllUsers = "\all users"
' This will display the path of the users profiles
'WScript.Echo sprofilesroot
' Now enumerate all existing user profile folders
Set oFolder = oFSO.GetFolder(sProfilesRoot)
Set colFSOSubfolders = oFolder.Subfolders
End Sub
' ***************************************************************************************************************
' 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
WScript.Echo (strFoldername)
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
' ****************************************************************************************************************
On a side note if I run this outside of SCCM (on the local machines) it works.
-----------------------------------------------------code----------------------------------------------------------------------------------
Dim strComputer, VerOS, VerBig, Ver9x, Version9x, OS, OSystem
Dim objWMI, objItem, colItems
Dim oWMI, oWSH, oFSO
Dim sAllUsersProfile, oAllUsersFolder, sProfilesRoot, oFolder
Dim colFSOSubFolders, colWMISubFolders, colSubFolders, colSubFolders2
Dim intSize
Dim objFolder, objFolder2, objSubFolder
Dim strFolder, strFolderName
Dim objFile
Dim FolderToDelete
Dim AllUsers
Dim strCommandWin7, strCommandXP
On Error Resume Next ' this is set because the designated file may be locked from deletion
'Determin OS
' This is where WMI interrogates the operating system
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
' Here we filter Version
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
' Spot VerBig variable in previous section
' Note the output variable is called OSystem
Select Case VerBig
Case "6.1" OSystem = "Windows 7"
Case "6.0" OSystem = "Vista"
Case "5.2" OSystem = "Windows 2003"
Case "5.1" OSystem = "XP"
Case "5.0" OSystem = "W2K"
Case "4.0" OSystem = "NT 4.0**"
Case Else OSystem = "Unknown - probably Win 9x"
End Select
' This will display the complete Operating System version
'Wscript.Echo "Version No : " & VerBig & vbCr & "OS System : " & OSystem
' Instantiate the various objects
strComputer = "."
Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
If OSystem = "Windows 7" Then
Call Win7
Else
Call XP
End If
' ***************************************************************************************************************
Sub Win7
'Search and destroy any file with @AIS in the name and send to c:\temp
'WScript.Echo ("Search and destroy any file with @AIS in the name and send to c:\temp")
strCommandWin7 = "cmd /c del c:\users\*@*ais*.txt /s > c:\temp\cookiedeleteWIN7.txt"
' This will display the command that will be run in a command promt
'WScript.Echo (strCommandWin7)
Set StdOut = WScript.StdOut
Set objShellWin7 = CreateObject("WScript.Shell")
set objScriptExecWin7 = objShellWin7.Exec (strCommandWin7)
sAllUsersProfile = oWSH.ExpandEnvironmentStrings("%USERPROFILE%")
Set oAllUsersFolder = oFSO.GetFolder(sAllUsersProfile)
sProfilesRoot = oAllUsersFolder.ParentFolder
FolderToDelete = "\appdata\locallow\sun\java\deployment\cache\6.0"
AllUsers = "\%userprofile%"
' This will display the path of the users profiles
'Wscript.Echo sprofilesroot
' Now enumerate all existing user profile folders
Set oFolder = oFSO.GetFolder(sProfilesRoot)
Set colFSOSubfolders = oFolder.SubFolders
End Sub
' ***************************************************************************************************************
'Search and destroy any file with @AIS in the name and send to c:\temp
'WScript.Echo ("Search and destroy any file with @AIS in the name and send to c:\temp")
strCommandXP = "cmd /c del C:\Docume~1\*@*ais* /s > c:\temp\cookiedirXP.txt"
' This will display the command that will be run in a command promt
'WScript.Echo strCommandXP
Set StdOut = WScript.StdOut
Set objShellXP = CreateObject("WScript.Shell")
set objScriptExecXP = objShellXP.Exec (strCommandXP)
' Get the Allusers profile folder path first and from this determine profiles parent folder
Set oAllUsersFolder = oFSO.GetFolder(sAllUsersProfile)
sProfilesRoot = oAllUsersFOlder.ParentFolder
FolderToDelete = "\Application Data\Sun\Java\Deployment\cache\6.0"
AllUsers = "\all users"
' This will display the path of the users profiles
'WScript.Echo sprofilesroot
' Now enumerate all existing user profile folders
Set oFolder = oFSO.GetFolder(sProfilesRoot)
Set colFSOSubfolders = oFolder.Subfolders
End Sub
' ***************************************************************************************************************
' Now go through each existing user profile folder looking for the designated folder to delete
if oFSO.FolderExists(sProfilesRoot & "\" & objSubfolder.Name & FolderToDelete) then
intSize = 0
strFolderName = sProfilesRoot & "\" & objSubfolder.Name & FolderToDelete
WScript.Echo (strFoldername)
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
' ****************************************************************************************************************
Posted by:
anonymous_9363
13 years ago
Look at your code: there is far too much duplication. The use of variables will reduce it considerably. For example, you can set the user profile location at the same place where you set the OSSystem variable.
Also, the use of WMI for file/folder manipulation is somewhat convoluted. Stick with the FileSystemObject object. There's lots of code around for it, particularly for recursing through files/folders. There are some good shells at http://www.computerperformance.co.uk
Also, the use of WMI for file/folder manipulation is somewhat convoluted. Stick with the FileSystemObject object. There's lots of code around for it, particularly for recursing through files/folders. There are some good shells at http://www.computerperformance.co.uk
Posted by:
prasath
13 years ago
Hey mike. the best way to delete the folder in short code. use this script
Go to notepad and type this.
Dim a
Set a = CreateObject("Scripting.FileSystemObject")
a.DeleteFolder("c:\Documents and Settings\Humm")
save the document as xx.vbs. click on the vbs file and see.. The file will be deleted
Regards
Prasath(09944917252)
Go to notepad and type this.
Dim a
Set a = CreateObject("Scripting.FileSystemObject")
a.DeleteFolder("c:\Documents and Settings\Humm")
save the document as xx.vbs. click on the vbs file and see.. The file will be deleted
Regards
Prasath(09944917252)
Posted by:
fageeh
13 years ago
Posted by:
anonymous_9363
13 years ago
One, please don't cross-post. If your question hasn't been answered, then perhaps no-one is able to help you.
Two, there must be a quadzillion code samples on the web for handling file dates. Use a search engine.
Two, there must be a quadzillion code samples on the web for handling file dates. Use a search engine.
Posted by:
MarverickMY
13 years ago
Posted by:
anonymous_9363
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.