Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Temp Files and Folders 2

Status
Not open for further replies.

Brycspain

IS-IT--Management
Joined
Mar 9, 2006
Messages
150
Location
US
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
 
Do you have any other programs open? I just tried it, got an error, closed out Outlook, and was able to run the program without incidence.....Just a thought....
 
[1] .delete method is applicable to folder exactly the same as to file, in case you don't know. If you delete folder using deletefolder method, you provide the path to it. Putting the obj as a parameter is to mislead yourself, though it works in this case as path is its default property.
>fso.DeleteFolder(oSubFolder)
[tt]fso.DeleteFolder oSubFolder.path,true[/tt]
or
[tt]oSubFolder.delete true[/tt]

[2] You can take note on which file you get permission denied so that you can investigate why. Like this.
> oFile.Delete True
Replace it by these.
[tt]on error resume next
oFile.Delete True
if err.number<>0 then
wscript.echo oFile.path & vbcrlf & err.description
err.clear
end if
on error goto 0
[/tt]

 
tfg13 is correct, if Outlook is open this script cannot delete the files as they are in use. I was planning on using this script as a logoff script so no programs would be open anyway.

Also, thanks Tsuji for your input as you're one of the best scripters I have ever seen on this board.

Also thanks to Marcdmac for his template.
 
I recommend using the DiskCleanup manager to do this job as it is designed to do it and can be scheduled to run when the user will not be impacted.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top