Hi,

I trying to get the LAST modified text file in a folder, but unsure how to do it.

I am using the following code:-

http://www.adrianxw.dk/SoftwareSite/FindFirstFile/findfirstfile3.html

#include <windows.h>
#include <iostream>

using namespace std;

int main()
{
   SYSTEMTIME SysTime;

   HANDLE hFind;
   WIN32_FIND_DATA FindData;

   cout << "A very basic FindFirst/Next demo.\n" << endl;

   // Find the first file

   hFind = FindFirstFile ( "//server4/data/Reports/MANAGER/*.txt", &FindData );
   cout << FindData.cFileName << " ";
   FileTimeToSystemTime ( &FindData.ftCreationTime, &SysTime );
   cout.width ( 2 );
   cout.fill ( '0' );
   cout << SysTime.wDay << "/";
   cout.width ( 2 );
   cout.fill ( '0' );
   cout << SysTime.wMonth << "/";
   cout << SysTime.wYear << " ";
   cout.width ( 2 );
   cout.fill ( '0' );
   cout << SysTime.wHour << ":";
   cout.width ( 2 );
   cout.fill ( '0' );
   cout << SysTime.wMinute << ":";
   cout.width ( 2 );
   cout.fill ( '0' );
   cout << SysTime.wSecond;
   cout << endl;

   // Look for more

   while ( FindNextFile ( hFind, &FindData ) )
   {
      cout << FindData.cFileName << " ";
      FileTimeToSystemTime ( &FindData.ftCreationTime, &SysTime );
      cout.width ( 2 );
      cout.fill ( '0' );
      cout << SysTime.wDay << "/";
      cout.width ( 2 );
      cout.fill ( '0' );
      cout << SysTime.wMonth << "/";
      cout << SysTime.wYear << " ";
      cout.width ( 2 );
      cout.fill ( '0' );
      cout << SysTime.wHour << ":";
      cout.width ( 2 );
      cout.fill ( '0' );
      cout << SysTime.wMinute << ":";
      cout.width ( 2 );
      cout.fill ( '0' );
      cout << SysTime.wSecond;
      cout << endl;
   }
   FindClose ( hFind );
   getchar();

   return 0;
}

Will I have to create my own date and time class to decide which text file is the most recent or is there an easier way?

Edited 6 Years Ago by iamthwee: n/a

#include <iostream>
#include <windows.h>

int main()
{
    WIN32_FIND_DATA info;
    HANDLE file = FindFirstFile("*.txt", &info);

    if (file != INVALID_HANDLE_VALUE) {
        struct FileInfo {
            HANDLE h;
            WIN32_FIND_DATA info;
        } newest;

        newest.h = file;
        newest.info = info;

        while (FindNextFile(file, &info)) {
            if (CompareFileTime(&info.ftLastWriteTime, &newest.info.ftLastWriteTime) > 0) {
                newest.h = file;
                newest.info = info;
            }
        }

        std::cout << newest.info.cFileName << '\n';

        FindClose(file);
    }
}
Comments
Green candy for you.

^^ Spot on.

Welcome back. Have you lost the urge to talk about yourself in the third person now ;)

> Have you lost the urge to talk about yourself in the third person now
Without that defining quirk Ed would be just another wannabe expert that nobody remembers. ;)

This question has already been answered. Start a new discussion instead.