Hi folks,

I am using FindNextFile() and GetFileTime() to find a file in a folder with the latest creation time. The code is as follows...

WIN32_FIND_DATA ffd;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    DWORD dwError=0;
    LPWSTR filePattern[MAX_PATH];
    LARGE_INTEGER filesize;
    FILETIME ftCreate;
    FILETIME maxCreationTime;
    maxCreationTime.dwLowDateTime=0;
    maxCreationTime.dwHighDateTime=0;

    hFind = FindFirstFile((LPCWSTR)filePattern, &ffd);

   if (INVALID_HANDLE_VALUE == hFind) 
   {
      DisplayErrorBox(TEXT("FindFirstFile"));
      return dwError;
   }

   // List all the files in the directory with some info about them.

   do
   {
      if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
      {
         _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);
      }
      else
      {
         filesize.LowPart = ffd.nFileSizeLow;
         filesize.HighPart = ffd.nFileSizeHigh;
         _tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
         if (INVALID_HANDLE_VALUE != hFind) {
            if(GetFileTime(hFind, &ftCreate, NULL, NULL))
             {
                if( CompareFileTime( &ftCreate, &maxCreationTime) == 1 )
                {
                    maxCreationTime =  ftCreate;
                    cacheCAB = ffd.cFileName;
                }
            }
            else
            {
               dwError = GetLastError();
               cerr << "Error: GetFileTime returned " << dwError << endl;     
            }
         }   
      }
   }
   while (FindNextFile(hFind, &ffd) != 0);

<snip>

The call to GetFileTime() fails with a return code of 6 ( invalid file handle ). Is it incorrect to pass "hFind" as the first parameter to GetFileTime()? I am guessing that GetFileTime() cannot be passed the search file handle returned bu FindFirstFile()/FindNextFile()? Any thoughts on how I can use GetFileTime() in this context?

>>Is it incorrect to pass "hFind" as the first parameter to GetFileTime()?

No. You need to read msdn for the functions you want to use. Read this. It clearly states the hFile parameter must have been obtained by CreateFile().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.