Directory maintenence

I need a sub routine that will look at the contents of a directory...figure out witch is the oldest file and delete it.....approximatley 10 files in the directory...

zirlancelot@hotmail.com

ZirLancelotAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nil_dibCommented:
language?
OS?
compiler?
.....
0
KangaRooCommented:
ZirLacelot are you still here?
0
mikeblasCommented:

#include <windows.h>


BOOL DeleteOldest(LPCTSTR pstrDirectory)
{
   TCHAR sz[_MAX_PATH];
   lstrcpy(sz, pstrDirectory);
   lstrcat(sz, "\\*.*");

   WIN32_FIND_DATA findData;
   HANDLE hFindHandle = FindFirstFile(sz, &findData);

   // find anything at all?
   if (hFindHandle == INVALID_HANDLE_VALUE)
      return FALSE;

   double dOldest = -1.0;
   TCHAR szOldest[_MAX_PATH];

   do
   {
      if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
         continue;

      SYSTEMTIME sysTime;
      FileTimeToSystemTime(&findData.ftLastWriteTime, &sysTime);

      double dVariantTime;
      SystemTimeToVariantTime(&sysTime, &dVariantTime);

      if (dOldest == -1.0 || dVariantTime < dOldest)
      {
         dOldest = dVariantTime;
         lstrcpy(szOldest, findData.cFileName);
      }
   }
   while (FindNextFile(hFindHandle, &findData));

   FindClose(hFindHandle);

   // directory was empty
   if (dOldest == -1.0)
      return FALSE;

   TCHAR szEraser[_MAX_PATH * 2];
   lstrcpy(szEraser, pstrDirectory);
   lstrcat(szEraser, "\\");
   lstrcat(szEraser, szOldest);

#if 0
   return DeleteFile(szEraser);
#else
   return TRUE;
#endif
}


void main()
{
   DeleteOldest("C:\\Test");
   DeleteOldest("C:\\Booger");
   DeleteOldest("C:\\Windows");
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.