0

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 by iamthwee: n/a

3
Contributors
5
Replies
8
Views
6 Years
Discussion Span
Last Post by Radical Edward
Featured Replies
  • 1

    [QUOTE=iamthwee] I trying to get the [I]LAST modified[/I] [/QUOTE] Then you want to use [icode]ftLastWriteTime[/icode]. [QUOTE] to decide which text file is the most recent[/QUOTE] You can directly compare the FILETIMEs using [URL="http://msdn.microsoft.com/en-us/library/ms724214%28v=VS.85%29.aspx"]CompareFileTime()[/URL] Read More

  • [code] #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 = … Read More

1
#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);
    }
}
Votes + Comments
Green candy for you.
0

^^ Spot on.

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

0

> 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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.