Question : 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.

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

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.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
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
Loop
Random Solutions  
 
programming4us programming4us