All Forums >> [Scripting] >> WSH & Client Side VBScript >> Another delete temp files script advice needed Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
I "discovered" VBScripting about 6 months ago and almost blew a cranial artery with how much time some of the more basic scripts saved me. Logon, defrag, automate- oh my. I can only start by sayingI wholeheartedly appreciate the scripts I use now and written by those with seemingly endless knowledge compared to my own.
I sorta hacked together a delete temp file script from many different sources and intended for it to delete all temporary files in
C:\Documents and Settings\*USERNAME*\Recent C:\Documents and Settings\*USERNAME*\Local Settings\Temp C:\Documents and Settings\*USERNAME*\Local Settings\Temporary Internet Files\ C:\C:\Documents and Settings\*USERNAME*\Local Settings\Temporary Internet Files\Content.IE5
and run a counter while in each folder then log the end result deleted file count for each directory. The Event is logged as such:
77 Recent files were deleted 151 Temp files were deleted 1 Internet Files were deleted Script ran for 0.31 seconds.
I have since given up on improving this script due to the fact that no matter what I do to the "Internet Files" portion, the deleted file count never gets higher than "1", it will delete all of the thousands of files in that directory but it never counts 'em. So I was wondering if anyone could look over this and possibly offer some advice as to what my malfunction is in regards to the file count? Also maybe some general advice as to what I could do different/better?
Back on topic(and the whole point); 1. Could I ask for some help on the file count thing? 2. And any "big brotherly" advice on the general code itself?
I know it's messy. Go ahead, snicker or flat out laugh. If I could find a printer that used finger paint, I'd use it to print this all out and slap it on the fridge. Oh yeah, I have ZERO programming/scripting/coding experience whatsoever, I'm a hardware guy turning network/system admin. I may not know all of the terms or names of stuff, but it seems to make sense in my head.
< Message edited by colossus610 -- 5/13/2008 2:40:43 PM >
Thank you, all squared away now. Your help also made me realize that it is counting the files regardless of whether it's successfully deleting them or not......will look into this. As for the general code itself, anything I could be doing better?
< Message edited by colossus610 -- 5/14/2008 11:35:51 AM >
(1) don't use "On Error Resume Next" globally, but restrict it to the line(s) you are expecting problems from
(2) don't mix Subs/Functions into your top level code as you did here:
... Next 'Delete all emptied SubFolders Sub KillSubFolders (SubPath) objFSO.DeleteFolder SubPath End Sub endTime = Timer ...
(3) where do you check for emptiness of the folder you are going to delete?
(4) don't rely on default properties: this
Call KillSubFolders (objSubFolder)
passes a folder object to KillSubFolders; this
objFSO.DeleteFolder SubPath
expects the name of the folder to delete. Thinking about the datatypes can result in better code: e.g. why not use the SubFolder's .Delete method instead of FSO.DeleteFolder?
(5) use better variable names; "i" may be ok in a counting loop, but the entity you named "f" is too important for your script to be named in such a disrespectful way.
(6) Consider to keep the Dim, Init, and Use of your variables as close together as possible: e.g. you don't need startTime (why not dtStart like objFSO?) before the loop, so
[a long list of Dims] ... lots of code (1) .... Dim startTime : startTime = Timer ... lots of code (2) .... Dim runTime : runTime = round(Timer - startTime, 2) ... lots of code (3) .... WScript.Echo .... runTime ...
This way you have to remember/think of runTime across one 'lots of code' block, not three.