Tuesday, July 8, 2008

Delete temporary files easily and quickly


In order to delete temporary files on a Windows machine, there are many quick CLI commands, such as rmdir "%temp%". Depending on the size of the %temp% folder though, this command can take a bit to run. If you place it into a login script, you'll end up delaying a user's login while their temp folder is being cleaned.

To reduce the time needed for deleting temporary files, I suggest using a vbscript instead.

Copy the following and save it into a .vbs file such as 'cleantemp.vbs'. When you run it, it will erase all files inside your temp folder quickly and silently.

'This script deletes all files not in use in the 'target folder' (change line 8 to select something other than temp)
Option Explicit
On Error Resume Next

Dim target, objShell, strEnv, newfso, fso, objFiles, strFile, oSubfolder

Set objShell = CreateObject("Wscript.Shell")
target = objShell.ExpandEnvironmentStrings("%temp%")
Set fso = CreateObject("Scripting.FileSystemObject")

Set newfso = fso.GetFolder(target)
Set objFiles = newfso.Files
For each strFile in objFiles

For Each oSubFolder In newfso.SubFolders
fso.DeleteFolder oSubFolder

DiggIt!Add to del.icio.usAdd to Technorati Faves

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories