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

不羁的心 提交于 2021-02-07 17:24:41

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/29880039/finding-the-last-created-file-in-the-directory-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!