Script To Delete Windows Temporary Files

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.

Free eBook!

Like what you read?

If so, please join over 28,000 people who receive our exclusive weekly newsletter and computer tips, and get FREE COPIES of 5 eBooks we created, as our gift to you for subscribing. Just enter your name and email below:

Post A Comment Using Facebook

  • http://shanx.com Shanx

    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.

  • Robert

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

  • Jason Faulkner

    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.

  • http://shanx.com Shanx

    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.

  • http://1timePost Justin Hierholzer

    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!

  • printman

    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.

    • http://www.chad.com Chad

      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”

  • sp33dyt

    This delete the temp file for the current user
    what about if there are more than an user?

    • Joske

      Then u put the script in all users startup folder for every pc of your network

      suc6

  • bdtech

    How do I only delete certain temp files, for example: windowstemp files and Internet temp files. I ran the bat file, it deleted lot of windows .dll files and now my computer won't start up.

What’s Your Preference?

Daily Alerts

Each day we send out a quick email to thousands of PCMECH readers to notify them of new posts. This email is just a short, plain email with titles and links to our latest posts. You can unsubscribe from this service at any time.

You can subscribe to it by leaving your email address in the following field and confirming your subscription when you get an email asking you to do so.

Enter your email address for
Daily Updates:

Weekly Newsletter

Running for over 6 years, the PCMECH weekly newsletter helps you keep tabs on the world of tech. Each issue includes news bits, an article, an exclusive rant as well as a download of the week. This newsletter is subscribed to by over 28,000 readers (many who also subscribe to the other option) - come join the community!

To subscribe to this weekly newsletter simply add your email address to the following field and then follow the confirmation prompts. You will be able to unsubscribe at any time.

Enter your email address for
Free Weekly Newsletter: