Member Avatar for iamthwee

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?

Recommended Answers

All 5 Replies

I trying to get the LAST modified

Then you want to use ftLastWriteTime .

to decide which text file is the most recent

You can directly compare the FILETIMEs using CompareFileTime()

commented: ty +11
Member Avatar for iamthwee

Thanks is it possible to code up a quick example to compare two files.

Thanks

#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);
    }
}
commented: Green candy for you. +11
Member Avatar for iamthwee

^^ 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. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.