Get LastWriteFileTime - GetLastTime() kernel32.dll function

 

Hi Programmers!


I would like to get the last write time of a file. I tried this code:

#import "kernel32.dll"
   int _lopen  (string path, int of);
   int _lclose (int handle);
   int GetFileTime (int hFile, int lpCreationTime, int lpLastAccessTime,  int lpLastWriteTime );

int handle = _lopen("C:\\FX\\file.dat", 0);

int FileTime ;

if (GetFileTime(handle, 0, 0, FileTime) == false)
   Print("ERROR");
else
   Print ("LastWriteTime is: ",FileTime);

_lclose (handle);

The result is always zero. Zero and not false. So I get the last print message: "LastWriteTime is: 0" - I don't know why.


Where do I go wrong? How can I fix it to work well?


Relative

 
Relative wrote >>

Hi Programmers!

I would like to get the last write time of a file. I tried this code:

The result is always zero. Zero and not false. So I get the last print message: "LastWriteTime is: 0" - I don't know why.

Where do I go wrong? How can I fix it to work well?

Relative

The parameters in GetFileTime point to Win32 FILETIME structures. Structures are difficult to read in MT4 because all we have at our disposal is arrays of int. A quick search for FILETIME in this forum reveals https://www.mql5.com/en/articles/1543 but you may find a better one.

Paul

http://paulsfxrandomwalk.blogspot.com/

 
phampton:

The parameters in GetFileTime point to Win32 FILETIME structures. Structures are difficult to read in MT4 because all we have at our disposal is arrays of int.

The easiest thing is probably to convert the FILETIME to a SYSTEMTIME, along the lines demonstrated by the following.


No error checking in any of this, and it may return UTC rather than local time. Can't tell at the moment because London is currently GMT+0, and the documentation isn't 100% explicit. Currently too busy for proper code comments in the following, I'm afraid.


#import "kernel32.dll"
   int _lopen  (string path, int of);
   int _lclose (int handle);
   int GetFileTime (int hFile, int & lpCreationTime[], int & lpLastAccessTime[],  int & lpLastWriteTime[]);
   int FileTimeToSystemTime(int & lpFileTime[], int & lpSystemTime[]);
#import

int start()
{
   // Open file and get time - NO ERROR CHECKING in any of this
   int handle = _lopen("whatever...", 0);
   int ftCreation[2], ftAccess[2], ftWrite[2];
   GetFileTime(handle, ftCreation, ftAccess, ftWrite);
   _lclose(handle);

   // Convert the creation FILETIME into a SYSTEMTIME
   int tmCreation[4];
   FileTimeToSystemTime(ftCreation, tmCreation);
   
   // Extract the WORD values in the structure into MQL ints
   int lYear = (tmCreation[0] & 65535);
   int lMonth = tmCreation[0] >> 16;
   int lDay = tmCreation[1] >> 16;
   int lHour = (tmCreation[2] & 65535);
   int lMinute = tmCreation[2] >> 16;
   int lSecond = (tmCreation[3] & 65535);
}
 

Oh! It seems a bit complex, but I'm working on it...


Thank you for your help!

Reason: