Discussion of article "MQL5 Programming Basics: Files" - page 2

 
Artyom Trishkin:

Only in the mql5 help there is no word about them yet....

Will there be for 4?

FileSave has already been in the help since the 14th of FileSave.

You can update manually from the site

 
Andrey F. Zelinsky:

A small comment on the comprehensibility of the reference.

The position of the reference was not immediately clear (there is an obvious understatement):

You should read the help before criticising as usual. If the example does not help, then any reference is powerless

void OnStart()
  {
   string  filename=_Symbol+"_ticks.bin";
   MqlTick ticks[];
//---
   int copied=CopyTicks(_Symbol,ticks,COPY_TICKS_ALL,0,ticks_to_save);
   if(copied!=-1)
     {
      PrintFormat(" CopyTicks(%s) copied %d ticks",_Symbol,copied);
      //--- if the tick history is synchronised, the error code is zero
      if(!GetLastError()==0)
         PrintFormat("%s: Ticks are not synchronized, error=%d",_Symbol,copied,_LastError);
      //--- write the ticks to the file
      if(!FileSave(filename,ticks,FILE_COMMON))
         PrintFormat("FileSave() failed, error=%d",GetLastError());
     }
 
Andrey F. Zelinsky:

If you like, I can write nothing at all, except admiration. If I start admiring, you'll say I'm being sarcastic and so on. I can't be pleased.

If you understand the phrase:


I understand

intcommon_flag=0// fileflag, by default files are written to the <data catalogue>\MQL5\Files\ folder.

 
Artyom Trishkin:

Only in the mql5 help there is no word about them yet....

Will there be for 4?

Unfortunately, nothing will be available in the fourth version.
 
Let's write a useful function to get a list of files and folders

Unfortunately, the function does not provide a complete list (including all subfolders and files in them). That's why I'll add it

string GetDirectory( const string FileName )
{
  int Pos = StringFind(FileName, "\\");
  int LastPos = Pos;
  
  while (Pos >= 0)
  {
    LastPos = Pos;
    
    Pos = StringFind(FileName, "\\", Pos + 1);
  }

  return((LastPos >= 0) ? StringSubstr(FileName, 0, LastPos + 1) : "");
}

// Returns the list of all files by filter
int GetFileNames( string &FileNames[], int Pos = 0, string Filter = "*", const int Common_Flag = 0 )
{
  string FileName;
  const long handle = FileFindFirst(Filter, FileName, Common_Flag);

  if (handle != INVALID_HANDLE)
  {    
    const string Directory = GetDirectory(Filter);    
    Filter = StringSubstr(Filter, StringLen(Directory));
    
    do
    {
      const string TmpFileName = Directory + FileName;
      
      ArrayResize(FileNames, Pos + 1);      
      FileNames[Pos] = TmpFileName;      
      Pos++;
        
      if (!FileIsExist(TmpFileName, Common_Flag))
        Pos = GetFileNames(FileNames, Pos, TmpFileName + Filter, Common_Flag);
    }
    while (FileFindNext(handle, FileName));  
    
    FileFindClose(handle);
  }
  
  return(Pos);
}

The meaning of the filter is the same as in the article. Example of application

// Logs the entire sandbox structure
void OnStart()
{
  string FileNames[];
  
  const int Amount = GetFileNames(FileNames);
  
  for (int i = 0; i < Amount; i++)
    Print(FileNames[i]);
}
 

To one place and on the topic

Forum on trading, automated trading systems and testing trading strategies

Features of mql5 language, subtleties and techniques of work

fxsaber, 2017.08.15 17:30

// Move a folder. Description is the same as FileMove - https://www.mql5.com/en/docs/files/filemove
bool FolderMove( const string FolderNameSrc, const string FolderNameDst, const int Common_Flag = 0, const int Mode_Flags = FILE_REWRITE )
{
  if (FolderNameSrc == FolderNameDst)
    return(false);

  string FileName;
  const long handle = ::FileFindFirst(FolderNameSrc + "\\*", FileName, Common_Flag);

  bool Res = false;

  if (handle != INVALID_HANDLE)
  {
    do
    {
      const string TmpFileNameSrc = FolderNameSrc + "\\" + FileName;
      const string TmpFileNameDst = FolderNameDst + "\\" + FileName;

      if (::FileIsExist(TmpFileNameSrc, Common_Flag))
        Res = ::FileMove(TmpFileNameSrc, Common_Flag, TmpFileNameDst, Mode_Flags);
      else
      {
        const string TmpFileNameSrc2 = ::StringSubstr(TmpFileNameSrc, 0, ::StringLen(TmpFileNameSrc) - 1);
        const string TmpFileNameDst2 = ::StringSubstr(TmpFileNameDst, 0, ::StringLen(TmpFileNameDst) - 1);

        if (!FolderMove(TmpFileNameSrc2, TmpFileNameDst2, Common_Flag, Mode_Flags))
          Res = ::FolderCreate(TmpFileNameDst2, Mode_Flags & FILE_COMMON);
      }
    }
    while (::FileFindNext(handle, FileName));

    ::FileFindClose(handle);
  }

  return(::FolderDelete(FolderNameSrc, Common_Flag) && Res);
}
 

this article is great, thank you very much for the information.

 

There is a typo in the article. In the description of the function "Read a file with delimiters into an array" the description of the structure:

struct SLine{
   string line[];
};

and in should be (and in the script so):

struct SLine{
   string field[];
};
 

А как можно уменшить размер файла? Примерно имею какой то BIN файл в которой поддерживаю какие то данни, потом делаю дефрагмент файла и наконец хочу умешнит его длина, потому что сзади уже есть лишное пространство. Как етого сделать? Нужна какая та функция типа FileResize(int newSize).

 
A huge human THANK YOU for the article, especially for #property tester_file, without it I wouldn't have succeeded and would have been beating my head against the wall for a long time.