[RESOLVED] Best practice for deleting temp files (not internet cache)
I'd like to delete Windows temporary files in code, and was hoping there was a Windows API call for doing this as opposed to just using Kill or whatever. Unless that's the best way?
I've been using my own homegrown disk cleanup utility for years now, which deletes unwanted cookies, internet history, url typeahead, and temporary internet files. Plus it empties the recycle bin.
But I was just poking around my user profile and noticed the following folder:
C:\Documents and Settings\Ellis\Local Settings\Temp
This folder currently contains 10,786 files in 49 folders, using up 564 megs. This is surely slowing down my virus scanning if nothing else.
So I'd like to add the cleaning of that to my disk cleanup routine, but unfortunately it doesn't appear to be enumerated by SHGetFolderPath.
What is the best way to clear that path?
EDIT: Also, if there's a system-wide temp directory, as opposed to the user-specific temp files like in the above example, I'd like to be able to clear that too.
Re: Best practice for deleting temp files (not internet cache)
See if this helps...
http://www.hushpage.com/Visual_Basic/viper/viper.html
Else you will have to use SHFileOperation API... Give me a moment... I will give you an example
Edit:
See post 4
http://www.vbforums.com/showthread.p...HFileOperation
Re: Best practice for deleting temp files (not internet cache)
Quote:
Originally Posted by
koolsid
Else you will have to use SHFileOperation API... Give me a moment... I will give you an example
I always do; I just referenced "Kill" because it paints the picture more effectively.
Re: Best practice for deleting temp files (not internet cache)
:lol:
I took kill in the vb6 way... The only problem with using kill is since Local Settings\Temp is a hidden folder, it might give you problem deleting it...
BTW, I had already posted a link... You were posting when I was editing :)
Re: Best practice for deleting temp files (not internet cache)
Great link. As the case with so many VB6 projects' source code online, the numbskull didn't include shared modules in his zip. Not a huge deal, though, as I think I can identify and pull out what I need from it. I'll post the code I end up with for posterity.
Re: Best practice for deleting temp files (not internet cache)
Quote:
Originally Posted by
Ellis Dee
Great link. As the case with so many VB6 projects' source code online, the numbskull didn't include shared modules in his zip.
I have booted in Vista so I don't have access to vb6 and hence I can't check it... lolzzz
Quote:
Not a huge deal, though, as I think I can identify and pull out what I need from it. I'll post the code I end up with for posterity.
I am sure, you will ;)
Re: Best practice for deleting temp files (not internet cache)
Not filled to brimming with confidence so far. To delete system temp files, the linked code does the following:
Code:
KillFiles Environ("windir") & "\Temp"
Sub KillFiles(Path As String)
Dim strFile As String
strFile = Dir(Path & "\*.*"
While strFile <> ""
Kill strFile
strFile = Dir
Wend
End Sub
It actually uses While...Wend, Kill, and Environ(). Plus, none of the modules have Option Explicit, and a whole lot of other suboptimal programming practices. He even hardcoded "Temp" in there for the Windows temp folder. Ugh.
Most irritating is that those are the only non-internet temp files it erases. It doesn't delete anything under User\Local Settings\Temp.
I was really hoping that there was an official way to do this sanctioned by Microsoft, as opposed to just going directly to the folders and killing all the files I see.
Re: Best practice for deleting temp files (not internet cache)
I guess, it is best to stay with SHFileOperation API
Re: Best practice for deleting temp files (not internet cache)
Let's take a step back and see if I can at least identify the folders without hardcoding them. I enumerated all folders returned by SHGetFolderPath:
Code:
xp.Folder.EnumerateFoldersToDebugWindow
2: C:\Documents and Settings\Ellis\Start Menu\Programs
5: C:\Documents and Settings\Ellis\My Documents
6: C:\Documents and Settings\Ellis\Favorites
7: C:\Documents and Settings\Ellis\Start Menu\Programs\Startup
8: C:\Documents and Settings\Ellis\Recent
9: C:\Documents and Settings\Ellis\SendTo
11: C:\Documents and Settings\Ellis\Start Menu
13: C:\Documents and Settings\Ellis\My Documents\My Music
14: C:\Documents and Settings\Ellis\My Documents\My Videos
16: C:\Documents and Settings\Ellis\Desktop
19: C:\Documents and Settings\Ellis\NetHood
20: C:\WINDOWS\Fonts
21: C:\Documents and Settings\Ellis\Templates
22: C:\Documents and Settings\All Users\Start Menu
23: C:\Documents and Settings\All Users\Start Menu\Programs
24: C:\Documents and Settings\All Users\Start Menu\Programs\Startup
25: C:\Documents and Settings\All Users\Desktop
26: C:\Documents and Settings\Ellis\Application Data
27: C:\Documents and Settings\Ellis\PrintHood
28: C:\Documents and Settings\Ellis\Local Settings\Application Data
31: C:\Documents and Settings\All Users\Favorites
32: C:\Documents and Settings\Ellis\Local Settings\Temporary Internet Files
33: C:\Documents and Settings\Ellis\Cookies
34: C:\Documents and Settings\Ellis\Local Settings\History
35: C:\Documents and Settings\All Users\Application Data
36: C:\WINDOWS
37: C:\WINDOWS\system32
38: C:\Program Files
39: C:\Documents and Settings\Ellis\My Documents\My Pictures
40: C:\Documents and Settings\Ellis
41: C:\WINDOWS\system32
43: C:\Program Files\Common Files
45: C:\Documents and Settings\All Users\Templates
46: C:\Documents and Settings\All Users\Documents
47: C:\Documents and Settings\All Users\Start Menu\Programs\All Programs\Administrative Tools
48: C:\Documents and Settings\Ellis\Start Menu\Programs\All Programs\Administrative Tools
53: C:\Documents and Settings\All Users\Documents\My Music
54: C:\Documents and Settings\All Users\Documents\My Pictures
55: C:\Documents and Settings\All Users\Documents\My Videos
56: C:\WINDOWS\resources
59: C:\Documents and Settings\Ellis\Local Settings\Application Data\Microsoft\CD Burning
No value returns C:\Windows\Temp, which is a downer. And no value returns C:\Documents and Settings\Ellis\Local Settings\Temp. Even worse, no value even returns C:\Documents and Settings\Ellis\Local Settings, meaning I'd have to do something like get the Local History folder, back up one level, and then put a hardcoded "\Temp" on the end of it.
All of that is like a throwback to the 90s. I haven't used hardcoded system paths in so many years I can't even remember.
Is there a more robust API than SHGetFolderPath I can use to identify more system paths?
EDIT: Though I only went up to 255. Does SHGetFolderPath() take values larger than 255?
Re: Best practice for deleting temp files (not internet cache)
Eli try this for me... since I cannot test it...
Code:
Private Declare Function GetTempPath Lib "kernel32" Alias _
"GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Const MAX_PATH = 255
'~~> Retuns something like C:\DOCUME~1\user\LOCALS~1\Temp\
Public Function GetWinDowsTempDir() As String
Dim sRet As String, lngLen As Long
'~~> Create buffer
sRet = String(MAX_PATH, 0)
lngLen = GetTempPath(MAX_PATH, sRet)
If lngLen = 0 Then Err.Raise Err.LastDllError
GetWinDowsTempDir= Left$(sRet, lngLen)
End Function
'~~> Use the API GetFullPathName to use GetWinDowsTempDir to get the full path
Re: Best practice for deleting temp files (not internet cache)
You continue to be all kinds of awesome; that returns the short path of the user temp file, the one in \User\LocalSettings\Temp. Which totally rocks.
GetFullPathName doesn't return the long name, btw; it returns the full path and filename of a specified file, particularly useful if you're using relative paths. If you send it a short name, you get a short name back. Luckily my XP Library contains a LongFileName routine:
vb Code:
Private Const MAX_PATH = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Function GetLongName(ByVal Folder As String) As String
Dim strToken As String
Dim lngHandle As Long
Dim strReturn As String
Dim lngTemp As Long
Dim typ As WIN32_FIND_DATA
If Right$(Folder, 1) = "\" Then Folder = Left$(Folder, Len(Folder) - 1)
Do While lngHandle <> -1
lngHandle = FindFirstFile(Folder, typ)
strToken = Left$(typ.cFileName, InStr(typ.cFileName, vbNullChar) - 1)
If Len(Folder) > 2 Then
strReturn = strToken & "\" & strReturn
lngTemp = InStrRev(Folder, "\")
If lngTemp > 0 Then Folder = Left$(Folder, lngTemp - 1)
Else
strReturn = Folder & "\" & strReturn
Exit Do
End If
lngTemp = FindClose(lngHandle)
Loop
lngTemp = FindClose(lngHandle)
'Strip away unwanted characters.
GetLongName = Left$(strReturn, Len(strReturn) - 1)
End Function
Re: Best practice for deleting temp files (not internet cache)
I ran "Disk Cleanup" from Accessories to see if there was anything else I was missing, and I see they say outright that anything in a temp folder that's older than a week is safe to delete. Armed with that, I'm going to just iterate the temp folders and delete all the files with a DateDiff() to Now() of more than 7 days using the regular file/folder deletion code in my XP library. (SHFileOperation)
The only remaining task before I mark this thread resolved is to identify the system temp folder (C:\Windows\Temp) without having to hardcode "Temp" on the end of the Windows folder. Any ideas? Anyone? Bueller...
Re: Best practice for deleting temp files (not internet cache)
Ellis
No ideas, unfortunately.
I'm just jumping on board as a lurker.
Great thread so far... :thumb:
Lord knows, I sorely need to do something very similar.
Spoo
Re: Best practice for deleting temp files (not internet cache)
Quote:
You continue to be all kinds of awesome; that returns the short path of the user temp file, the one in \User\LocalSettings\Temp. Which totally rocks.
Lolzzzz.... :lol:
I usually don't give up but when I posted "post 10", I was already half asleep. It was approx 6 AM in the morning. I was awake all night doing a freelance job...
You can also use this in lieu of post 10!!!! SSS (Short Sweet and Simple) :cool:
Code:
MsgBox IIf(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))
Quote:
The only remaining task before I mark this thread resolved is to identify the system temp folder (C:\Windows\Temp) without having to hardcode "Temp" on the end of the Windows folder. Any ideas? Anyone? Bueller...
Would this help? ;)
Code:
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias _
"GetWindowsDirectoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long
Private Sub Command2_Click()
Dim buffer As String * 512, length As Integer
Dim TmpPath As String
length = GetWindowsDirectory(buffer, Len(buffer))
TmpPath = Left$(buffer, length) & "\Temp"
If Not Dir(TmpPath, vbDirectory) = vbNullString Then
MsgBox "Directory Temp INSIDE Windows exists. The path is " & TmpPath
Else
MsgBox "There is no Temp Directory INSIDE Windows Directory"
End If
End Sub
Re: Best practice for deleting temp files (not internet cache)
I'm not a fan of the Environ() function for a number of reasons, even though I am aware that the GetTempPath() is actually just a wrapper for the environment variable.
As for hardcoding the Temp directory under Windows, yeah, that's pretty much how I'm going to implement it barring a nice API way to return that folder.
NOTE: Check out what I found in my reading up on GetTempPath:
Quote:
The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:
•The path specified by the TMP environment variable.
•The path specified by the TEMP environment variable.
•The path specified by the USERPROFILE environment variable.
•The Windows directory.
Note that it doesn't check whether or not the path actually exists or can be written to, so you may end up trying to write your log files to a path that doesn't exist, or one that you cannot access.
heh, so it's not like I can just go willy-nilly delete the contents of the temp path anyway. Oops, sorry for deleting your entire user profile; you didn't want that, did you? Or even worse, say goodbye to your Windows folder. Doh!
What kind of check should I do to ensure the safe operation of this procedure? Best solution I have is to only clear the temp folder (and all subfolders under it) if:
LCase(Right(TempPath, 5)) = "\temp"
So I'm hardcoding it in there anyway, no matter what. This makes me not care so much that I will be hardcoding \Temp under Windows, and for that matter I might as well check for a C:\Temp as well.
Given this, I'm calling this thread resolved. Thanks much for your help, sid. I'd rep you if it let me.
Re: [RESOLVED] Best practice for deleting temp files (not internet cache)
Quote:
Thanks much for your help, sid. I'd rep you if it let me.
That's OK :wave: The thought counts :)
What more important is that your query is solved ;)
tc
Sid
Re: [RESOLVED] Best practice for deleting temp files (not internet cache)
I've also seen (and found correct according to convention) that files following the pattern ~*.tmp older than a week are safe to delete. So many disk cleaning softwares go one step ahead and implement this.
vb Code:
If file Like "~*.tmp" Then
' check if it is older than a week and delete it here
End If