Love Jeff's PowerShell command, but for an alternative vbs solution for Windows machines without PowerShell you could try the following.
Save as <filename>.vbs
and execute:
<filename>.vbs <target_dir> <NoDaysSinceModified> [Action]
The third parameter, [Action]
is optional. Without it the files older than <NoDaysSinceModified>
will be listed. With it set as D
it will delete files older than <NoDaysSinceModified>
Example
PurgeOldFiles.vbs "c:\Log Files" 8
will list all files in c:\Log Files
older than 8 days old
PurgeOldFiles.vbs "c:\Log Files" 8 D
will delete all files in c:\Log Files
older than 8 days old
note: this is a modified version of Haidong Ji's script on SQLServerCentral.com
Option Explicit
on error resume next
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim iDaysOld
Dim fAction
sDirectoryPath = WScript.Arguments.Item(0)
iDaysOld = WScript.Arguments.Item(1)
fAction = WScript.Arguments.Item(2)
Set oFSO = CreateObject("Scripting.FileSystemObject")
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files
If UCase(fAction) = "D" Then
'Walk through each file in this folder collection.
'If it is older than iDaysOld, then delete it.
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If
Next
else
'Displays Each file in the dir older than iDaysOld
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
Wscript.Echo oFile.Name & " " & oFile.DateLastModified
End If
Next
End If
'Clean up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing
Set fAction = Nothing