1.

Solve : Creating a Batch file to remove files in a folder?

Answer» QUOTE from: Quintin on April 10, 2008, 03:13:52 PM
I would like to create a batch file that executes on the first of the MONTH to remove files in a folder older than 30 days. These files are log files.

Thanks,

Quintin
here's a vbscript
Code: [Select]Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\temp")
For Each efile in objFolder.Files
'Wscript.Echo "file is " & efile
'WScript.Echo eFile.DateLastModified
If DateDiff("d",eFile.DateLastModified,Now) >= 30 Then
WScript.Echo "file FOUND that is 1 month old: " & efile
WScript.Echo eFile.DateLastModified
objFSO.DeleteFile(eFile)
End If
Next

save it as script.vbs and on command line
Code: [Select]c:\test> cscript /nologo script.vbs
Deleting files more than 30 days old means that on the 1st of every month following a 31-day month, (the majority of months) the files from the 1st of the previous month vanish too. If the objective is merely to save space, this may not matter, but if ***all*** of the previous month's files ***must*** be kept, then this might be a problem.

The VB script worked perfectly. I have it scheduled to run nightly. The only problem will be with the password. Whenever I CHANGE my network credentials I'll have to go to the server and reset the password.

Thank you very much for your script.

Quintin


Discussion

No Comment Found