Script To Delete Windows Temporary Files

Posted Feb 16, 2008 | by Jason Faulkner  

While doing some system maintenance the other day, I noticed my Windows Temp directory had an unusually large number of files in it (over 1 GB). Upon browsing through the files, most simply appeared to be garbage.

Unlike the temporary Internet files, there isn’t an obvious way to limit the size of the temp directory, so instead I wrote up a batch file to clear out the folder for me. The only line in the file is:

del /q /f /s %temp%\*

I saved this file as “clean_temp.bat” and scheduled it to run monthly. This should work in 2000/XP/2003/Vista.

Which Of These Traits Applies To YOUR Computing Life?...

7 Responses to “Script To Delete Windows Temporary Files”

  1. Shanx says:

    Much nicer than these piecemeal hacks is to download CCleaner (free) and run it. It cleans up a whole lot of stuff beyond the temp folder, optimizes your registry, allows you to cleanly uninstall gunk software, and so forth.

  2. Robert says:

    About Clean temp , I like it the script but, without the quote…otherwise it will not work with the quote.

  3. Jason Faulkner says:

    Robert, thanks for pointing that out. When I checked what my script is, there were no quotes so I’m not sure why I added them… habit I guess.

    Shanx:
    I am not a big fan of CCleaner because:
    1) I can clean temp files myself with the above script.
    2) My browser manages the size of it’s own cache.
    3) I NEVER mess with the registry when I don’t have to. “Cleaning”/”Optimizing” your registry, in my opinion is an extremely high risk with relatively low reward action.
    4) I can’t really think of anything else I would need to really clean up. Something like removing log files or Windows Update temp files, I don’t do anyway since they don’t take up much space.

    Additionally, I don’t see how using simple native OS functions is a “piecemeal” hack.
    I respect your opinion… to each their own.

  4. Shanx says:

    You’re right. We’re all comfortable with different things.

    Piecemeal in the sense that you have to write an external script to use functions that should have a much simpler interface.

    Cleaning the registry is a huge benefit in terms of Windows performance. Microsoft itself recommends that you use their free RegClean utility (part of Powertoys). This is particularly relevant for those of us who install and uninstall utilities all the time. Other than the most basic grandma users of Windows, I suspect this includes pretty much everyone.

    CCleaner cleans a lot of things:
    http://www.ccleaner.com/

    I use four browsers for testing and CC cleans up after all of them. Only the things I request though — not the entire cache. For example, I don’t want to clean my authenticated sessions in Firefox which is my main browser and I can set CC up to leave them alone.

    Anyway, my point was not to belittle your suggestion. It’s always nice to learn nifty little tricks. But there’s a difference between novelty of suggestion and actual practice. For the latter, I prefer CCleaner to do its job, which it does well.

    PS. Not associated with CC in any way.

  5. Good luck using CCleaner when it changes your
    Windows System files, and you are left with
    “Windows cannot find the file HAL.DLL”!!
    We had this nasty “portable” version cause all
    kinds of issues for our managed clients.

    Like I said – Good luck reinstalling Windows

    PS: If your lucky and know that ccleaner has
    messed up your system, DO NOT REBOOT – run the
    System Restore utility to go back!

  6. printman says:

    wow. i cant believe how much i do NOT know, which is a relief, at the least i know i dont know that much..

    im A+ certfied.. read two different books thoroughly before i went for the exam.. currently i work as a net admin.. those books never taught me how to write scripts like that.. how do i learn that?

    thanks.

    • Chad says:

      OPTION EXPLICIT

      ‘ purpose: delete *.tmp files in documents and settings
      ‘ where *.tmp files located at marix

      dim strTempFolder ‘ path to user temp folder
      dim strProfileRoot ‘ documents and settings root folder

      ‘ **********************************************************************

      private sub DeleteTMPFiles()
      ‘ delete .tmp files in user’s temp dir

      dim fso
      dim fsoFolder
      dim fsoFiles
      dim strFile
      dim objShell ‘ wshell
      dim objFiles
      dim strEnviron ‘ environment var
      dim strExt ‘ file extension
      dim NumFiles ‘ file count
      Dim theSubFolders ‘ subfolders in directory
      Dim fld ‘ folder in collection
      Dim FolderNames
      dim test
      dim logFiles
      dim htmFiles
      dim htmlFiles
      dim emfFiles
      dim hlpFiles
      dim sqmFiles
      dim txtFiles
      dim bmpFiles
      dim pngFiles
      dim gifFiles

      NumFiles = 0

      FolderNames = “”

      ‘ set shell
      SET objShell = CreateObject(”Wscript.Shell”)

      ‘ get environment var
      strEnviron = objShell.ExpandEnvironmentStrings(”%temp%”)
      ‘msgbox strEnviron, vbokonly, “Environ”

      test = strEnviron & “\*.tmp”
      logFiles = strEnviron & “\*.log”
      htmFiles = strEnviron & “\*.htm”
      htmlFiles = strEnviron & “\*.html”
      emfFiles = strEnviron & “\*.emf”
      hlpFiles = strEnviron & “\*.hlp”
      sqmFiles = strEnviron & “\*.sqm”
      txtFiles = strEnviron & “\*.txt”
      bmpFiles = strEnviron & “\*.bmp”
      pngFiles = strEnviron & “\*.png”
      gifFiles = strEnviron & “\*.gif”

      ‘msgbox test, vbokonly, “Environ + *.tmp”

      ‘ open cmd to run DIR
      ‘objShell.Run “cmd /K Dir *.TMP”

      ‘msgbox “cd ” & test

      ‘ close cmd window on completion – C
      objshell.run “cmd /C del ” & test
      objshell.run “cmd /C del ” & logFiles
      objshell.run “cmd /C del ” & htmFiles
      objshell.run “cmd /C del ” & htmlFiles
      objshell.run “cmd /C del ” & emfFiles
      objshell.run “cmd /C del ” & hlpFiles
      objshell.run “cmd /C del ” & sqmFiles
      objshell.run “cmd /C del ” & txtFiles
      objshell.run “cmd /c del ” & bmpFiles
      objshell.run “cmd /c del ” & pngFiles
      objshell.run “cmd /c del ” & gifFiles

      ‘ keep cmd window open – K
      ‘objShell.Run “cmd /K cd ” & strEnviron

      set objShell = nothing

      end sub

      ‘ **********************************************************************

      ‘ Main code
      ‘ **********************************************************************

      DeleteTMPFiles()

      ‘msgbox “Deleted *.TMP files in user Temp directory”, 48, “Done”

Leave a Reply