Coding Thread, .bat file clear old files in Coding and Web Development; Hi All,
Been working on another project.
I am looking for a piece of code, to put in a .bat ...
I am looking for a piece of code, to put in a .bat file, that does the following:
*Filename* is stored like the following example "19-01-2010--14-51.sql"
----
Looks at the current date
If C:\LoginTrackerBackups\*Filename* is 7 days or older than the current date
DeleteFile
Else
Move onto the Next file
End
----
You should really store your filenames like "2010-10-29--14-15.sql", not "19-01-2010--14-51.sql"
Even so, it's tricky to do the date arithmetic in a script, more so in a batch file.
All is not lost, though. If the age of the file is correct, you can use ROBOCOPY, which makes short work of tasks like these.
This will move all files older than 7 days to MyTemp, then delete MyTemp.
I am hoping that the MINAGE part is going to work. This is something I cannot test until this time next week, as I dont have a file in that folder that is more than a week old. Does it look at the File Modified Date attribute to work out if it is over a week old?
The attached script should do it. Although it's a VBScript you can use CScript to run it in a batch file. Run 'cscript PurgeFiles.vbs /?' from a command prompt for help.
Another alternative (if you don't mind relying on third-party programs) is DelOld.