4

Even though I searched there isn't any problem like mine in the internet. my problem is, I want to get the name of the last file created in the directory. My system will create /.png files coming from my Camera in the directory of this code and I want my code to the take last created one. I want 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 didnt use extra libraries I used the following one as it can be 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 get 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  

getCreationTime method take one parameter as

Parameters: pTimeStamp: A pointer to a FILETIME structure containing the time the file was created.

refTime: A reference to a CTime object.

Elektrik Adam
  • 58
  • 1
  • 6

1 Answers1

2

I think I fixed your code. Had 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;
}
Databyte
  • 1,420
  • 1
  • 15
  • 24
  • bro, what can I say, I love you, (kisses) – Elektrik Adam Apr 26 '15 at 17:07
  • but I have a question why did you used // name = finder.GetFileName().GetString() Is there a big difference I cannot see? – Elektrik Adam Apr 26 '15 at 17:08
  • GetFilename() returns a [CString](https://msdn.microsoft.com/en-us/library/aa315043). But your function requires to return a std::string (name is of that type). In order to convert the CString to std::string I call GetString(), which is a method of CString. Note that GetString() itself returns a const char pointer which can then be assigned to name. – Databyte Apr 26 '15 at 20:17