Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Directory list files and delete after certain date 1

Status
Not open for further replies.

LizardKingSchwing

Programmer
Oct 12, 2001
96
NL



Hellooo..

This should not be too diff but -

I need to write a function in VC++ / MFC which lists files in a directory and then checks each file listed for its creation date and deletes it if it is created b4 a specified date.

Any samples / examples etc. would be grrreatttt


ThanX
 
Disclaimer:
This was not compiled or tested. Make sure the century comparison works correctly. I've forgotten if there might be a Y2K bug in the CTime library?

Using MFC, do something like this:

{
CFileFind InFiles;
CString sPathAndFile;
CString sCommand;

BOOL bMoreFiles = InFiles.FindFile( "*.*" );
CTime tRightNow = CTime::GetCurrentTime();

while ( bMoreFiles )
{
bMoreFiles = InFiles.FindNextFile();

CTime tCreationTime;

if ( Infiles.GetCreationTime( &tCreationTime ) )
{
if ( tCreationTime.GetYear() <= tRightNow.GetYear() - 2 )
{
// more comparisons for month, day, hour, minute...

cout << InFiles.GetFilePath();
sCommand.Format( &quot;del %s&quot;, InFiles.GetFilePath() );

// comment out until you've tested!
// system( (LPSTR&) sCommand );
}
}
}
}

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top