Discussion Locked

Command to delete files older than 1 day

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I would like to run a batch file to delete certain files that are older the 1 day on an NT Server. Does anyone know the syntax that I can use for this?

7 replies

by way_good_guy

There is no batch file to do it, but qbasic is on all nt platforms, you can write qbasic bas routine to do it
Make a call to attrib and read results
or
Use a windows scripting tool to get file properties.

by dc1

Try the following VB code.

Dim objArgs

Set objArgs = WScript.Arguments
on error resume next

intDays = objArgs(0)

wscript.echo "deleting files older than " & intDays & " days old."
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.GetFolder("c:your directory here")
Set fc = f.Files
For Each f1 in fc
modDate = f1.DateLastModified
sysDate = Date

If DateDiff("d", modDate, sysDate) >= Cint(intDays) then
f1.delete
End if
Next

Check on MSDN.microsoft.com for other vbscripts for admin tasks like these

by mckaytech

This is kind of a funky workaround, but the xcopy command will copy all the files of a certain date and forward so you could theoretically copy the files you want to keep to a safe location, delete all the files in the original location and then copy the good ones back.

Or you could use the /L switch with xcopy to populate a list of the files to save and then run that list through a batch file using exception logic.

Or you could use DIR /TA > filelist.txt to populate a list of files by last access time (or /TC for creation date/time or /TW for last time written), run that list through a sort and then use that to run the delete process.

Gee, that VB script is starting to look awfully good....

paul

This discussion is closed to new replies.