Delete Files from Temp Directory that are at least 2 hours old

Is there a Windows utility or method not using vb script that can delete files from a temporary directory that are at least 2 hours old or more.
Who is Participating?
Here's a way to do it with VBScript.

Paste the script below into a text file with a .vbs extension.  Customize the value of the strRoot variable on line 1 with the location of the temporary folder.  To include files in subfolders, change the value of the blnSubFolders variable on line 3 to True.

Running the script (e.g. with cscript filename.vbs) will echo the actions to be performed.  Once you have tested it successfully, remove the apostrophe from line 29 to delete the files.

Note that this deletes files permanently, without moving them to the Recycle Bin.

strRoot = "c:\temp"
strMask = "*.*"
blnSubFolders = False
intHours = 2
strCommand = "cmd /c @echo off & dir " & Chr(34) & _
    strRoot & "\" & strMask & Chr(34) & " /a:-d /b"
If blnSubFolders Then
    strCommand = strCommand & " /s"
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec(strCommand)
Set objStdOut = objWshScriptExec.StdOut
Do Until objStdOut.AtEndOfStream
    strFile = objStdOut.ReadLine
    If Not blnSubFolders Then
        strFile = strRoot & "\" & strFile
    End If
    Set objFile = objFSO.GetFile(strFile)
    If DateDiff("h", objFile.DateLastModified, Now) > intHours Then
        WScript.Echo "Delete " & strFile
        'objFile.Delete True
    End If

Open in new window

Use CCleaner program to delete file sfrom temp directory. the program is free to use and download and it is recommended for all user and it has supported xp and vista
bmichaelhAuthor Commented:
Thank you.

I should have added that I need to run as a batch file so that I can run it as a scheduled job.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Joseph DalyCommented:
Ok so this is what im thinking. Its basically a two step process there is only one part of it that I dont have figured out yet.

Robocopy has an option to only move files older than a certain amount of time. I believe the switch  you will want to use is /mingage

/MINAGE:n : MINimum file AGE - exclude files newer than n days/date

What you would do is have robocopy run and move the files in your folder that are older than a certain date to another folder. Once they were in there you could use the del command to delete the folders. The batch file would be something similar to below.

The only thing I have to figure out is how to specify hours in the robocopy move command instead of days.
robocopy /sourcedire /targetdir /mov /MINAGE:n 
del targetdir\*.* /F /Q

Open in new window

Hi bmichaelh

The expert batch files in this question will give you some idea of the complexities involved with reliably using dates and times in pure "DOS" batch:


Calculating Days from the %DATE% is hard enough when you consider the different number of days in different months, but calculating the Hours or Minutes using the %TIME% causes problems of its own because of the different time formats and calculating back past midnight.

Compare this to the apparent simplicity of a VBScript:

Why is it that you can't use a *.vbs file?
I was googling and typing while Shift-3 was posting ;-)
Bill PrewCommented:
Here's a prior question that has a (lengthy) batch script approach to the question you asked.  It could probably be made to do what you need, or perhaps after you see the complexity involved you'll decide to pursue a non batch approach.

If you still want to pursue a batch script approach let me know.

Bill PrewCommented:
Sorry, hit SUBMIT too sooon, here's the link.

Steve KnightIT ConsultancyCommented:
Personally I'd do this in VBScript too... or a at a push and having to be seen to be using a batch file then use a batch file that gets VBScript to do the hard work of checking the file age. The one on my page below is checking for a file being there in the last 20 mins but the same sort of principal:!OpenDocument

Resource kit command forfiles.exe only works on whole days old as far as I remember (don't have it on this machine) otherwise that would be an option too.

bmichaelhAuthor Commented:
If I use VBScript and run this on a server.  Is there anything that needs to be enabled on the server for this to run?
Steve KnightIT ConsultancyCommented:
It shouldn't be an issue on 2000 or 2003 servers.  I haven't tried VBScript on a 2008 one yet so can;t say either way.

Try this as a simple test on one of your servers:

x=inputbox("please enter something")
wscript.echo x

And save it as test.vbs

then double click it from explorer on the server or from a command prompt using:

cscript //nologo test1.vbs

David Johnson, CD, MVPOwnerCommented:
vbscript works fine in ws2008/w7
David Johnson, CD, MVPOwnerCommented:
vbscript works fine in ws2008/w7
U can simply right a batch file to Delete Junk file

Following file types are considered as junk files : *.~* ~*.* *.??~ *. *.tmp *._mp *.old *.bak *.syd *.chk *.gid mscreate.dir *.dmp 0???????.nch
You can make a batch file that scans for them and deletes them with the following content:
del c:\*.~* /s /q
del c:\~*.* /s /q
del c:\*.??~ /s /q
del c:\*. /s /q
del c:\*.tmp /s /q
del c:\*._mp /s /q
del c:\*.old /s /q
del c:\*.bak /s /q
del c:\*.syd /s /q
del c:\*.chk /s /q
del c:\*.gid /s /q
del c:\mscreate.dir /s /q
del c:\*.dmp /s /q
del c:\0???????.nch /s /q
del c:\windows\temp\*.* /q
del c:\temp\*.* /q
del d:\*.~* /s /q
del d:\~*.* /s /q
del d:\*.??~ /s /q
del d:\*. /s /q
del d:\*.tmp /s /q
del d:\*._mp /s /q
del d:\*.old /s /q
del d:\*.bak /s /q
del d:\*.syd /s /q
del d:\*.chk /s /q
del d:\*.gid /s /q
del d:\mscreate.dir /s /q
del d:\*.dmp /s /q
del d:\0???????.nch /s /q
Add commands for other partitions in a similar fashion. Again, type del /? at the command prompt for more about this command.

Chandar Singh
Thank you bmichaelh.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.