You can also checkout Kahless_9's delete_older utility at
http://www.shankodev.com/Tools/items/delete_older/delete_older_Page_00.htm
Apparently this is a task that is commonly required on backend servers where work related process and log files may accumulate, hence the exact utility delete_older is provided for among some other usefull ones.
The source code for this utility is provided and can be obtained from the frameworks [Tools\cleanup_utils\xxx_older\delete_older].
The crux of this utilities code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
void CConsoleMain::Process()
{
KDirTraverser dirTrav;
dirTrav.SetFileSpec(m_strFileSpec);
dirTrav.SetRecurtSubDirs(m_bProcessSubDirs);
dirTrav.SetFileExecInfoFunc(_ProcessFileFunc, this);
dirTrav.SetPreDirChangeInfoFunc(_ProcessEnterSubDirFunc, this);
dirTrav.SetPostDirChangeInfoFunc(_ProcessLeaveSubDirFunc, this);
dirTrav.Execute();
}
void CConsoleMain::_ProcessFileFunc(const KStr & strDir, const _finddata_t & fileinfo, LPVOID pParam)
{
CConsoleMain * inst = (CConsoleMain *)pParam;
KDate dtFileLastMod = KDate(fileinfo.time_write).GetDate();
if (inst->m_dtDeleteOlderThanDate > dtFileLastMod)
{
int nResult = _unlink((LPCTSTR)(strDir+"\\"+fileinfo.name));
if (nResult)
{
printf("unable to delete file [%s] due to [%s]\n", fileinfo.name, strerror(errno));
return;
}
printf("deleted file [%s]\n", fileinfo.name);
}
}
|
The linux version is slightly different in that it does not invoke a callback function that accepts a _finddata_t structure as this is windows specific.
Instead the linux version invokes the callback function that passes the filename as a string. The callback function then uses this filename to retrieve its file times via a call to KFileIO::GetFileInfo
The linux and windows version could both have been made exacty like the linux version but I suppose the windows version was developed to take advantage of the extra features provided from the windows version of the framework.
One thing I've also noticed when using [delete_older] on a server is that you may afterward end up with a number of empty folders.
To clear these you could use the frameworks [erase_empty] utility for which you should also be able to get the source code for.