[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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

0
ZirLancelot
Asked:
ZirLancelot
1 Solution
 
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

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now