Ive researched every post on this board with regards to deleting temp files, recursion, using filesystemobject(used Google and Technet),etc and used Markdmac's folder/subfolder deleting routine as a template. Of course this is beginners coding as I am a novice and was wondering why I get permission denied errors when attempting to delete files as I try to debug this script. I am a local administrator on my XP Pro SP2 workstation so running it on my own profile as I'm logged in shouldn't be a big deal. Could someone please give me a hint on what I'm doing wrong? Thanks in advance.
Code:
'On Error Resume Next
'Declare variables
Dim fso
Dim oFolder1
Dim oFolder2
Dim oFolder3
Dim oSubFolder1
Dim oSubFolder2
Dim oSubFolder3
Dim colSubfolders1
Dim colSubfolders2
Dim colSubfolders3
Dim oFile
Dim userProfile
Dim Windir
'Set up environment
Set WSHShell = CreateObject("WScript.Shell")
Set fso = createobject("Scripting.FileSystemObject")
userProfile = WSHShell.ExpandEnvironmentStrings("%userprofile%")
Windir = WSHShell.ExpandEnvironmentStrings("%windir%")
'start deleting files
Set oFolder1 = fso.GetFolder(userProfile & "\Local Settings\Temp\")
For Each oFile In oFolder1.files
If DateDiff("d", oFile.DateCreated,Now) > 1 Then
oFile.Delete True
End If
Next
'Delete folders and subfolders
Set colSubfolders1 = oFolder1.Subfolders
For Each oSubfolder in colSubfolders1
If DateDiff("d", oSubFolder.DateCreated,Now) > 1 Then
fso.DeleteFolder(oSubFolder)
End If
Next
Set oFolder2 = fso.GetFolder(userProfile & "\Local Settings\Temporary Internet Files\")
For Each oFile In oFolder2.files
If DateDiff("d", oFile.DateCreated,Now) > 1 Then
oFile.Delete True
End If
Next
Set colSubfolders2 = oFolder2.SubFolders
For Each oSubfolder in colSubfolders2
If DateDiff("d", oSubFolder.DateCreated,Now) > 1 Then
fso.DeleteFolder(oSubFolder)
End If
Next
Set oFolder3 = fso.GetFolder(Windir & "\Temp\")
For Each oFile In oFolder3.files
If DateDiff("d", oFile.DateCreated,Now) > 1 Then
oFile.Delete True
End If
Next
Set colSubfolders3 = oFolder1.Subfolders
For Each oSubfolder in colSubfolders3
If DateDiff("d", oSubFolder.DateCreated,Now) > 1 Then
fso.DeleteFolder(oSubFolder)
End If
Next
'Clear memory
Set fso = Nothing
Set oFolder1 = Nothing
Set oFolder2 = Nothing
Set oFolder3 = Nothing
Set oSubFolder1 = Nothing
Set oSubFolder2 = Nothing
Set oSubFolder3 = Nothing
Set colSubfolders1 = Nothing
Set colSubfolders2 = Nothing
Set colSubfolders3 = Nothing
Set oFile = Nothing
Set userProfile = Nothing
Set Windir = Nothing
'End Script
WScript.Quit