1.

Solve : How do I delete files older than a modified date?

Answer»

We upgraded to Microsoft SQL Server 2005 and that program does not allow me to specify the deletion of backup files with a modified date older than two days. We're running on Windows Server 2003. Can somebody give me the code example for deleting old files in a directory? (We don't want to delete all the files; just those older than two days.)

Your help will be GREATLY appreciated.

Thanks,

Henry RobinetteCode: [Select]Set fso = CreateObject("Scripting.FileSystemObject")
Set fl = fso.GetFolder("your folder goes here")
Set fc = fl.Files
For Each f In fc
If DateDiff("d", f.DateLastModified, Now) > 2 Then
fso.DeleteFile f.Path, True
End If
Next

Replace your folder goes here with something more meaningful. Save the script with a vbs extension and run as cscript scriptname.vbs

This probably can be done in batch but date/time arithmetic in batch is a nightmare.

Good luck. 8-)
In case a DOS batch solution is desired. The main code can be as easy as this when using reusable DOS functions:

Code: [Select]@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

cd /d "%your folder goes here%"

for %%F in (*.*) do (
call:ftime tfile "%%F"
call:jdate tnow "%date%"
set /a diff=tnow-tfile
if !diff! GTR 2 DEL "%%F"
)

ECHO.&PAUSE&GOTO:EOF

:-----------------------------------------------------
:-- functions to be added below here
:-----------------------------------------------------
The functions itself can be COPIED from:
http://dostips.cmdtips.com/DtTipsDateTime.php

or simply download and modify this file:
http://dostips.cmdtips.com/BatchFTime.bat

Hope this INFORMATION is useful.



Discussion

No Comment Found