Finding the last created FILE in a directory, C ++

Despite what I have searched, there are no problems on the Internet like mine. my problem is I want to get the name of the last file created in the directory. My system will create the / .png files coming from my camera into the directory of this code and I want my code to be generated last. I want to do it with this code,

string processName()
{
   // FILETIME Date = { 0, 0 };
   // FILETIME curDate;
   long long int curDate = 0x0000000000000000;
   long long int date    = 0x0000000000000000;
   //CONST FILETIME date={0,0};
   //CONST FILETIME curDate={0,0};
   string name;
   CFileFind finder;
   FILETIME* ft; 

   BOOL bWorking = finder.FindFile("*.png");
   while (bWorking)
   {

      bWorking = finder.FindNextFile();

      date = finder.GetCreationTime(ft) ;

      //ftd.dwLowDateTime  = (DWORD) (date & 0xFFFFFFFF );
      //ftd.dwHighDateTime = (DWORD) (date >> 32 );


      if ( CompareFileTime(date, curDate))
      {
          curDate = date;
          name = (LPCTSTR) finder.GetFileName();
      }



   }
   return name;
}

      

I did not use additional libraries, I used the following one as seen in the link:

https://msdn.microsoft.com/en-US/library/67y3z33c(v=vs.80).aspx

In this code, I tried to give initial values ​​to 64 bit FILETIME variables and compare them with this while loop. However, I am getting the following errors.

1   IntelliSense: argument of type "long long" is incompatible with parameter of type "const FILETIME *"        44  25  
2   IntelliSense: argument of type "long long" is incompatible with parameter of type "const FILETIME *"        44  31  

      

The getCreationTime method takes one parameter as

Parameters: pTimeStamp: Pointer to a FILETIME structure containing the creation time of the file.

refTime: a reference to a CTime object.

+3


source to share


1 answer


I think I have fixed your code. It was necessary to change some types, etc .:



string processName()
{
    FILETIME bestDate = { 0, 0 };
    FILETIME curDate;
    string name;
    CFileFind finder;

    finder.FindFile("*.png");
    while (finder.FindNextFile())
    {
        finder.GetCreationTime(&curDate);

        if (CompareFileTime(&curDate, &bestDate) > 0)
        {
            bestDate = curDate;
            name = finder.GetFileName().GetString();
        }
    }
    return name;
}

      

+2


source







All Articles