Windows Vista Forums

delete temp files via script - can B added 2 startup

  1. #1
    pacinitaly's Avatar

    HappyGuru Lovin2Help

    Join Date : Jan 2009
    worst state in America
    Posts : 1,396
    VISTA Business 32bit SP2
    Local Time: 02:29 AM
    usa

    delete temp files via script - can B added 2 startup

    Good Morning, Afternoon,

    Will any experts please review this script and advise why it no longer works?
    ( actually, it will only work 1x when I first boot up and click it ) now, it only finds 1file @ 32kb. it's an OLD script from billsway and I have used it may times B4 on vista. ( Dang it, I can't make it work now to show you a pic ) you can try it, I'm not a gambler or a porno freak so all my files are clean- that's why I have no anti V or stuff like that.

    I have attached the file itself also.


    'A utility to delete XP and Vista temp files directory.
    'Put this file in Startup directory if you want.
    'It will Delete Temp file at LogOn.
    'Thanks to Bill James.
    'www.billsway.com.

    Option Explicit
    Dim fso, ws, Title
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ws = WScript.CreateObject("WScript.Shell")
    Title = "Temp File Cleaner"

    Dim TmpDir : TmpDir = ws.Environment("Process")("Temp")
    ChkTmpSafe
    Dim OldTmpSize : OldTmpSize = fso.GetFolder(TmpDir).size
    Dim arFiles(), cnt, dcnt, Fldr, SubFldr, File
    cnt = -1
    dcnt = 0
    DelTmpFiles TmpDir

    DelEmptyFldrs TmpDir
    Dim strF, strD, RptSize, TotSave
    CalcSave

    If dcnt >= 1 Then ws.Popup cnt & strF & dcnt & _
    strD & vbCrLf & vbCrLf & RptSize & vbCrLf & _
    vbCrLf & TotSave,60,Title

    Cleanup
    Sub ChkTmpSafe
    Dim Drv, Unsafe, WinDir, ComDir, PgmDir, SysDir, UnsafeDir
    If TmpDir = "" Then
    ws.Popup "Unsafe condition detected. %TEMP% " &_
    "Variable is not set.",60,Title,16
    Cleanup
    WScript.Quit
    End If
    If Not fso.FolderExists(TmpDir) Then
    fso.CreateFolder(TmpDir)
    Cleanup
    WScript.Quit
    End If
    For Each Drv In fso.Drives
    'If a drive or partition is not formatted, an error occurs
    On Error Resume Next
    If Drv.DriveType = 2 Or Drv.DriveType = 3 Then _
    Unsafe = Unsafe & Drv.RootFolder & "|"
    On Error GoTo 0
    Next
    Unsafe = Unsafe & fso.GetSpecialFolder(0) & "|"
    Unsafe = Unsafe & fso.GetSpecialFolder(0) & "\Command|"
    Unsafe = Unsafe & ws.RegRead("HKLM\Software\Microsoft" _
    & "\Windows\CurrentVersion\ProgramFilesPath") & "|"
    Unsafe = Unsafe & fso.GetSpecialFolder(1)
    Unsafe = Split(Unsafe, "|", -1, 1)
    For Each UnsafeDir In Unsafe
    If UCase(UnsafeDir) = UCase(TmpDir) Or _
    UCase(UnsafeDir) & "\" = UCase(TmpDir) Or _
    UCase(UnsafeDir) = UCase(TmpDir) & "\" Then
    ws.Popup "Unsafe condition detected. %TEMP% " &_
    "Variable is set to " & TmpDir,60,Title,16
    Cleanup
    WScript.Quit
    End If
    Next
    End Sub

    Sub DelTmpFiles(FldrSpec)
    Set Fldr = fso.GetFolder(FldrSpec)
    For Each File In Fldr.Files
    cnt = cnt + 1
    ReDim Preserve arFiles(cnt)
    Set arFiles(cnt) = File
    Next
    For Each SubFldr in Fldr.SubFolders
    DelTmpFiles SubFldr
    Next
    For Each File in arFiles
    On Error Resume Next
    File.Delete True
    If Err.Number = 0 Then dcnt = dcnt + 1
    Err.Clear
    Next
    End Sub

    Sub DelEmptyFldrs(FldrSpec)
    Set Fldr = fso.GetFolder(FldrSpec)
    For Each SubFldr in Fldr.SubFolders
    DelEmptyFldrs SubFldr
    Next
    On Error Resume Next
    If UCase(Fldr.Path) <> UCase(TmpDir) Then
    If Fldr.Files.Count = 0 Then
    If Fldr.SubFolders.Count = 0 Then
    Fldr.Delete
    End If
    End If
    End If
    If Err.Number = 76 Then
    Err.Clear
    On Error GoTo 0
    DelEmptyFldrs(TmpDir)
    End If
    End Sub

    Sub CalcSave
    Dim NewTmpSize, SaveSize, s1, s2
    Dim TmpClnLog, OldSave, HideLog, Log
    NewTmpSize = fso.GetFolder(TmpDir).size
    SaveSize = OldTmpSize - NewTmpSize
    s1 = " free space reclaimed."
    If SaveSize < 1024 Then
    RptSize = SaveSize & " bytes" & s1
    ElseIf SaveSize < 1048576 Then
    RptSize = Round(SaveSize / 1024) & " KB" & s1
    Else RptSize = Round(SaveSize / 1048576) & " MB" & s1
    End If
    Log = fso.GetSpecialFolder(0) & "\TempClean.Log"
    If Not fso.FileExists(Log) Then fso.CreateTextFile(Log)
    If fso.GetFile(Log).Size = 0 Then
    Set TmpClnLog = fso.OpenTextFile(Log, 8, True)
    TmpClnLog.WriteBlankLines(1)
    End If
    Set TmpClnLog = fso.OpenTextFile(Log, 1)
    OldSave = TmpClnLog.ReadLine
    If Not IsNumeric(OldSave) Then OldSave = 0
    TotSave = OldSave + SaveSize
    Set TmpClnLog = fso.OpenTextFile(Log, 2)
    TmpClnLog.WriteLine TotSave
    TmpClnLog.Close
    s2 = " reclaimed to date."
    If TotSave < 1024 Then
    TotSave = TotSave & " bytes" & s2
    ElseIf TotSave < 1048576 Then
    TotSave = Round(TotSave / 1024) & " KB" & s2
    Else TotSave = Round(TotSave / 1048576) & " MB" & s2
    End If
    cnt = cnt + 1
    If cnt = 1 Then strF = " file found, " _
    Else strF = " files found, "
    If dcnt = 1 Then strD = " file deleted." _
    Else strD = " files deleted."
    Set TmpClnLog = Nothing
    End Sub

    Sub Cleanup
    Set fso = Nothing
    Set ws = Nothing
    Set Fldr = Nothing
    End Sub



    Thank you all,

    ~Carmine


    PS: if you have any other .VBS files, please attach or send to me- I love shortcuts



      My System SpecsSystem Spec

  2. #2


    mayayana Guest

    Re: delete temp files via script - can B added 2 startup

    > PS: if you have any other .VBS files, please attach or send to me- I

    > love shortcuts
    >
    Here's one I wrote for my own purposes. The problem
    with XP+ is that the folder system is a mess. There are
    a number of possible locations for temp files:
    C:\Windows\TEMP
    C:\TEMP
    C:\Documents and Settings\[folder name]\Local Settings\TEMP

    I really don't see the point of having a logon script.
    If you've got that many undeleted temp files then you've
    probably got much bigger problems to worry about, since
    the majority of undeleted temp files come from things
    like software installations. I just leave this on the
    Desktop and run it once in a while. It checks all temp
    file locations listed above (assuming you have permission
    if your on an NTFS system) and concludes with a report of
    the volume deleted from each location.
    '---------------------------------------------------------------------------

    Dim sList, FSO2, sFol, Pt1, Pt2, Pt3, sCU, sBase, sTempPath, sPath1, oFol4,
    oFols4, OFolSub, Ret

    On Error Resume Next

    Ret = MsgBox("This script will delete files from all TEMP folders it can
    find. Other software should be closed first. Also, on NT systems (2000, XP)
    the script should be run by an administrator with full access to the file
    system." & vbCrLf & vbCrLf & "Do you want to proceed?", 33, "TEMP Cleaner")
    If Ret = 2 Then WScript.Quit

    '-- check for %WIN%\TEMP

    Set FSO2 = CreateObject("Scripting.FileSystemObject")
    sFol = FSO2.GetSpecialFolder(0) & "\TEMP"
    If (FSO2.FolderExists(sFol) = True) Then
    sList = CleanFiles(sFol) & vbCrLf
    sPath1 = sFol
    End If

    '-- check home drive ( ex.: C:\TEMP )
    sFol = Left(sFol, 3) & "temp"
    If FSO2.FolderExists(sFol) = True Then
    sPath1 = sFol
    sList = sList & CleanFiles(sFol) & vbCrLf
    End If

    '-- Get TEMP folder as the system sees it. On Win9x this is typically
    C:\Windows\TEMP.
    '-- On WinNT systems, unfortunately, the TEMP path is the current users TEMP
    folder
    '-- and there is no API for returning all TEMP folders.

    sFol = FSO2.GetSpecialFolder(2)
    If (FSO2.FolderExists(sFol) = True) And (sFol <> sPath1) Then
    sList = sList & CleanFiles(sFol) & vbCrLf
    End If

    '-- Find other user's, and all users, TEMP folders by walking the Docunemts
    and Settings folder tree.
    '-- There seems to be no official way to find the Documents and Settings
    folder, so this script is
    '-- presuming it's a parent folder of the current user's TEMP folder.

    Pt1 = InStr(1, sFol, ":\Docum", 1)
    Pt2 = InStr((Pt1 + 3), sFol, "\")
    Pt3 = InStr((Pt2 + 1), sFol, "\")

    '-- If no more paths found then quit here. It's probably either Win9x or
    someone running
    '-- without permission.

    If (Pt1 = 0) Or (Pt2 = 0) Or (Pt3 = 0) Then
    sList = "TEMP folders found: List shows beginning size of each TEMP
    folder found and size of that folder after cleaning." & vbCrLf & vbCrLf &
    sList
    MsgBox sList
    Set FSO2 = Nothing
    WScript.Quit
    End If

    '-- go through documents and settings subfolders to find TEMP
    '-- folder for each user.

    sBase = Left(sFol, Pt2) ' c:\documents and settings\
    sCU = Left(sFol, Pt3 - 1)
    sCU = Right(sCU, (len(sCU) - Len(sBase)))

    Set oFol4 = FSO2.GetFolder(sBase)
    Set oFols4 = oFol4.subfolders
    For Each OFolSub in oFols4
    If (OFolSub.Name <> sCU) Then
    sFol = sBase & OFolSub.name & "\Local Settings\Temp"
    If FSO2.FolderExists(sFol) = True Then
    sList = sList & CleanFiles(sFol) & vbCrLf
    End If
    End If
    Next
    Set oFols4 = Nothing
    Set oFol4 = Nothing

    sList = "TEMP folders found: List shows beginning size of each TEMP
    folder found and size of that folder after cleaning." & vbCrLf & vbCrLf &
    sList
    MsgBox sList
    Set FSO2 = Nothing
    WScript.Quit


    '-- END OF SCRIPT ------------------------------------------

    '-- Below here is the function called to delete files in each found TEMP
    folder.
    '-- This function is fairly simple. It just deletes subfolders and files,
    then formats
    '-- a return string that reports folder size before deletions and folder
    size after deletions.

    Function Cleanfiles(Path)
    Dim FSO, oFol, oFol2, oFols, oFils, oFil, Sz1, Sz2, Szi1, Szi2
    Set FSO = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set oFol = FSO.GetFolder(Path)
    Sz1 = oFol.Size
    Set oFols = oFol.SubFolders
    For Each oFol2 in oFols
    oFol2.Delete True
    Next
    Set oFols = Nothing
    Set oFils = oFol.Files
    For Each oFil in oFils
    oFil.Delete True
    Next
    Set oFils = Nothing
    Sz2 = oFol.Size
    Set oFol = Nothing
    Set FSO = Nothing

    Szi1 = " Bytes"
    If (Sz1 > 1024) Then
    Sz1 = Sz1 \ 1024
    Szi1 = " KB"
    End If
    If (Sz1 > 1024) Then
    Sz1 = Sz1 \ 1024
    Szi1 = " MB"
    End If

    Szi2 = " Bytes"
    If (Sz2 > 1024) Then
    Sz2 = Sz2 \ 1024
    Szi2 = " KB"
    End If
    If (Sz2 > 1024) Then
    Sz2 = Sz2 \ 1024
    Szi2 = " MB"
    End If

    CleanFiles = Path & ": " & Sz1 & Szi1 & " - " & Sz2 & Szi2 & vbCrLf

    End Function



      My System SpecsSystem Spec

  3. #3
    pacinitaly's Avatar

    HappyGuru Lovin2Help

    Join Date : Jan 2009
    worst state in America
    Posts : 1,396
    VISTA Business 32bit SP2
    Local Time: 02:29 AM
    usa

      Thread Starter

    Re: delete temp files via script - can B added 2 startup

    Thank you mayayana so much for the quick response,
    I'll look @ this later.

    your statement of and the advice:
    '-- Below here is the function called to delete files in each found TEMP
    folder.
    '-- This function is fairly simple. It just deletes subfolders and files,
    then formats
    '-- a return string that reports folder size before deletions and folder
    size after deletions.


    I need to read a bit

    chat later, need to sleep


    ~Carmine



      My System SpecsSystem Spec

  4. #4
    pacinitaly's Avatar

    HappyGuru Lovin2Help

    Join Date : Jan 2009
    worst state in America
    Posts : 1,396
    VISTA Business 32bit SP2
    Local Time: 02:29 AM
    usa

      Thread Starter

    Re: delete temp files via script - can B added 2 startup

    Cant make it work.

    thanks anyway

      My System SpecsSystem Spec

  5. #5


    mayayana Guest

    Re: delete temp files via script - can B added 2 startup

    > Cant make it work.

    >
    I've never had any problem with it. I use
    it regularly on both Win98 and XP. There's not
    really much to "work". It just chaecks for the
    existence of several folders and then deletes
    all content from them. But it does require you
    to be admin. on XP because it checks temp
    folders for individual users.

    In any case, that aspect is something to consider,
    whether or not you're using my script: On Win98
    you can delete temp files by cleaning out
    C:\windows\temp\. But on XP there are several possible
    locations, detailed in the script. A thorough temp file
    cleaner needs to check all of them.





      My System SpecsSystem Spec

delete temp files via script - can B added 2 startup

Similar Threads
Thread Thread Starter Forum Replies Last Post
CAN I delete the Temp folder El Mejor General Discussion 4 15 Aug 2009
Script to delete folder with specific name and files Star VB Script 1 11 May 2009
Script to delete files on shutdown fieseler VB Script 9 14 Nov 2008
Screen freeze after Startup with installed new ram added Tiff Vista hardware & devices 3 21 Jun 2007
delete doc temp files in vista drew Vista performance & maintenance 5 08 Jun 2007