I have a problem with getting the latest file of a directory.
My code works just fine unless there is only one file in that folder.
I am using the CFileFind
class to make this all happened. I looked at the Microsoft documentation and it says that .GetFileName
can only be called after FindNextFile
. If anybody has a solution I would be very thankful.
Here is my code:
std::string getLatestFile(std::string directory, const std::string& extension) {
FILETIME mostRecent = { 0, 0 };
FILETIME curDate;
std::string name;
CFileFind finder;
if (!CheckIfDirectory(directory))
return "";
ensureProperTermination(directory);//this just makes sure that the path is "\\" terminated
if (extension[0] == '.')
finder.FindFile((directory + "*" + extension).c_str());
else
finder.FindFile((directory + "*." + extension).c_str());
while (finder.FindNextFile())
{
finder.GetCreationTime(&curDate);
if (CompareFileTime(&curDate, &mostRecent) > 0)
{
mostRecent = curDate;
name = finder.GetFileName().GetString();
}
}
return directory + name;
}