How to get file's modifycation time?

 
Pls
 
 

Or write the time to the file when you modify it.

 
WHRoeder:

There is no mql4 methods.

Start with File Operations via WinAPI - MQL4 Articles and File Operations without Limitations - MQL4 Code Base

Then start researching m$ File Times (Windows)





THX, FileTime structure caused some surprise.

string GetFileTimeToStr (string filename, int WhatTime) //1: lpCreationTime 2: lpLastAccessTime 3: lpLastWriteTime
 {
  int handle=_lopen (filename,OF_READ); 
  int lpCreationTime[2]; int lpLastAccessTime[2]; int lpLastWriteTime[2];
  GetFileTime (handle, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
      
  int FileTimeInlpSystemTime[4];
  int lpTime[2];
  string WhatTimeStr;
  switch (WhatTime)
   { 
    case 1: lpTime[0]=lpCreationTime[0]; lpTime[1]=lpCreationTime[1]; WhatTimeStr="CreationTime"; break;
    case 2: lpTime[0]=lpLastAccessTime[0]; lpTime[1]=lpLastAccessTime[1]; WhatTimeStr="LastAccessTime"; break;
    case 3: lpTime[0]=lpLastWriteTime[0];  lpTime[1]=lpLastWriteTime[1]; WhatTimeStr="LastWriteTime"; break;
   }
  FileTimeToSystemTime(lpTime, FileTimeInlpSystemTime);
      
      
  int nYear=FileTimeInlpSystemTime[0]&0x0000FFFF;
  int nMonth=FileTimeInlpSystemTime[0]>>16;
  int nDay=FileTimeInlpSystemTime[1]>>16;
  int nHour=FileTimeInlpSystemTime[2]&0x0000FFFF;
  int nMin=FileTimeInlpSystemTime[2]>>16;
  int nSec=FileTimeInlpSystemTime[3]&0x0000FFFF;
      
  string time_string=WhatTimeStr+": "+FormatDateTime(nYear,nMonth,nDay,nHour,nMin,nSec);
      
  // ???? Alert(Symbol(), " handle:", handle, "     nYear:", nYear, " nMonth:", nMonth, " nDay:", nDay, " ", time_string); 
  _lclose (handle);
  return(time_string);
 }


 
ttechnik:

THX, FileTime structure caused some surprise.


#import "kernel32.dll"
   int _lopen  (string path, int of);
   int _lcreat (string path, int attrib);
   int _llseek (int handle, int offset, int origin);
   int _lread  (int handle, string buffer, int bytes);
   int _lwrite (int handle, string buffer, int bytes);
   int _lclose (int handle);
   int GetFileTime (int handle, int& lpCreationTime[], int& lpLastAccessTime[], int& lpLastWriteTime[]);
   bool FileTimeToSystemTime(int& lpFileTime[], int& lpSystemTime[]);
   
   int CreateDirectoryA(string path, int& atrr[]);
   
   void GetLocalTime(int& TimeArray[]);
   void GetSystemTime(int& TimeArray[]);
   int  GetTimeZoneInformation(int& TZInfoArray[]);
   bool CopyFileA (string source_file, string destination_file, bool if_exist);
   
   //The LPDWORD return values from GetDiskFreeSpace() are not the same as "double &". A DWORD is an unsigned 4-byte integer. 
   //A double is an 8-byte floating-point number.
   //Secondly, you cannot pass the address of a single int (or double) variable to a DLL in MQL4. You have to pass an array, 
   //of which only the first element then gets used.
   bool GetDiskFreeSpaceA  (string& as_dir, int& al_sectorspercluster[], int& al_bytespersector[], int& al_freeclusters[], int& al_totalclusters[]);
#import


int handle=_lopen (filename,OF_READ); 


_lopen not work, handle is -1

in MT4 Bulid 600

working good in 509


What is misstake?

 
ttechnik:


_lopen not work, handle is -1

in MT4 Bulid 600

working good in 509


What is misstake?


Please read some of the recent posts about using DLL functions . . .
 
ttechnik:


_lopen not work, handle is -1

in MT4 Bulid 600

working good in 509


What is misstake?


See https://www.mql5.com/en/forum/149304
 

THX,

I have seen it.

But I do not understand.

filename="C:"+BS+"example.txt";


I do not read this file, only open it...and read the modification date.

how to pass the filename variables to DLL?

 
ttechnik:

I do not read this file, only open it...and read the modification date.

how to pass the filename variables to DLL?

You cannot use _lopen() in v600 (or, strictly speaking, there is no easy way of doing so). DLL calls now use Unicode strings. There is no version of _lopen() which takes Unicode parameters because it is a legacy function from 16-bit Windows.

You need to use CreateFileW() instead of _lopen(). (File handles are interchangeable between _lopen/_lclose and CreateFile/CloseHandle because _lopen is simply a wrapper around CreateFile.)

#import "kernel32"
   int CreateFileW(string, uint, int, int, int, int, int);
   int GetFileTime (int handle, int& lpCreationTime[], int& lpLastAccessTime[], int& lpLastWriteTime[]);
   bool FileTimeToSystemTime(int& lpFileTime[], int& lpSystemTime[]);
   int CloseHandle(int);
#import
   
string GetFileTimeToStr(string filename, int WhatTime) //1: lpCreationTime 2: lpLastAccessTime 3: lpLastWriteTime
{
   string time_string = "";
   
   int handle = CreateFileW(filename, 0x80000000 /* GENERIC_READ */, 0, 0, 3 /* OPEN_EXISTING */, 0, 0); 
   
   if (handle == -1) {
      // Failed to open file 

   } else {
      int lpCreationTime[2]; int lpLastAccessTime[2]; int lpLastWriteTime[2];
      if (!GetFileTime (handle, lpCreationTime, lpLastAccessTime, lpLastWriteTime)) {
         // Failed to get file-time

      } else {
         int FileTimeInlpSystemTime[4];
         int lpTime[2];
         string WhatTimeStr;
         switch (WhatTime)
         { 
          case 1: lpTime[0]=lpCreationTime[0]; lpTime[1]=lpCreationTime[1]; WhatTimeStr="CreationTime"; break;
          case 2: lpTime[0]=lpLastAccessTime[0]; lpTime[1]=lpLastAccessTime[1]; WhatTimeStr="LastAccessTime"; break;
          case 3: lpTime[0]=lpLastWriteTime[0];  lpTime[1]=lpLastWriteTime[1]; WhatTimeStr="LastWriteTime"; break;
         }
         FileTimeToSystemTime(lpTime, FileTimeInlpSystemTime);
            
            
         int nYear=FileTimeInlpSystemTime[0]&0x0000FFFF;
         int nMonth=FileTimeInlpSystemTime[0]>>16;
         int nDay=FileTimeInlpSystemTime[1]>>16;
         int nHour=FileTimeInlpSystemTime[2]&0x0000FFFF;
         int nMin=FileTimeInlpSystemTime[2]>>16;
         int nSec=FileTimeInlpSystemTime[3]&0x0000FFFF;
            
         time_string=WhatTimeStr+": "+ FormatDateTime(nYear,nMonth,nDay,nHour,nMin,nSec);
      }
      CloseHandle(handle);   
   }

   return(time_string);
}
 
void OnStart()
{
   Print( GetFileTimeToStr("c:\\windows\\win.ini", 3) );
}

You may also need to bear in mind that the location of files created by MQL4 FileOpen() is now relative to TERMINAL_DATA_PATH, not TerminalPath().

 
gchrmt4:

You cannot use _lopen() in v600 (or, strictly speaking, there is no easy way of doing so). DLL calls now use Unicode strings. There is no version of _lopen() which takes Unicode parameters because it is a legacy function from 16-bit Windows.

You need to use CreateFileW() instead of _lopen(). (File handles are interchangeable between _lopen/_lclose and CreateFile/CloseHandle because _lopen is simply a wrapper around CreateFile.)

You may also need to bear in mind that the location of files created by MQL4 FileOpen() is now relative to TERMINAL_DATA_PATH, not TerminalPath().


Thank you, this code is perfectly.

I do not understand, what is different single BS os double BS?

It works booth.

filename="c:\windows\win.ini";
filename="c:\\windows\\win.ini";
 
ttechnik:

I do not understand, what is different single BS os double BS?


See https://docs.mql4.com/basis/types/string

MQL4 uses the same mechanism as many other languages such as C and Javascript where you can put special characters in a string constant by prefacing them with \

For example, "Column1\tColumn2" means "Column1 <tab> Column2". Similarly, "Column1\nColumn2" means "Column1 <new-line> Column2"

Because the \ character has a special meaning, you cannot just use \ in a string constant to mean "backslash". You have to use \\

\\ gets replaced with \ in the same way that \t gets replaced with <tab> and \n gets replaced with <new-line>

Therefore, if you do the following:

string filename="c:\\windows\\win.ini";
Print(filename);

...you should see the output "c:\windows\win.ini"

Strictly speaking, it is actually possible to do the following:

   string filename="c:\windows\win.ini";
   Print(filename);

...but this only works because \w has no meaning, and therefore MQL4 ignores it as a special character and treats it as separate \ and w characters

However, it is unwise to rely on this, and you should always use \\. For example, the following would not work as expected because \t does have a meaning:

   string filename="c:\windows\test.ini";
   Print(filename);