1.

Solve : Deleting file older that a certain date?

Answer»

Does anyone know how I can write a bat file to delete files in a directory that are more than say 31 days old?
This is for use on Windows 2000Batch language does not do date arithmetic unless you are prepared to write a boatload of IF statements.

This script may help you out:

Code: [Select]
dim fso, f, fs, fc
set fso = CreateObject("Scripting.FileSystemObject")
set f = fso.GetFolder("YourFolderGoeshere") ' <== CHG
set fc = f.files
for each fs in fc
if datediff("d", fs.datelastmodified, date) > 31 Then ' <== CHG
fso.deletefile fs, true
end if
next


Replace YourFolderGoesHere with a valid directory. You can also use DateLastAccessed or DateCreated in place of DateLastModified. Save the script with a VBS EXTENSION and run from the command line.

You could write a batch file using XCOPY to copy all files newer than 31 days to a temp directory, delete all files from your directory, then copy all the temp directory files back. Very DINKY and you WOULD have to hardcode a date.

Hope this helps. Looks good will give this a go. I had tried the xcopy option but the folder is receiving database archived logfiles from another LOCATION on a regular basis and I could not chance a transfer hitting the folder while it was being renamed and before a new folder had been created.
MANY thanks
Mike Tomlinson



Discussion

No Comment Found