Questions from Beginners MQL5 MT5 MetaTrader 5 - page 950

 

Thank you! It's not so easy there, so no way to do it without Win API?

 
Aleksey Vyazmikin:

Thank you! It's not so easy there, so no way to do it without Win API?

In this case, read the MQL5 documentation:

 
Aleksey Vyazmikin:

Thank you! It's not that simple, so you can't do it without Win API?

What's so complicated about it? I read two paragraphs and it all makes sense.

1. search for ALL ANY files in a specified directory, Common or terminal folder. You get the path to them, one by one.

2. Work with this line. Look for the specified folder in this line and count how many times the specified folder name occurs. This will be the number of files in the folder and its subfolders.

 
Aleksey Vyazmikin:
Can you please tell me how to get a list of directories in the specified directory MQL5\Files ?
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) : "");
}

// Получает список всех директорий (c поддиректориями) песочницы
int GetDirectories( string &Directories[], 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;

      if (!::FileIsExist(TmpFileName, Common_Flag) && (::GetLastError() == ERR_FILE_IS_DIRECTORY)) // https://www.mql5.com/ru/forum/1111/page2337#comment_9723503
      {
        ::ResetLastError();
        
        ::ArrayResize(Directories, Pos + 1, 10000);
        Directories[Pos] = TmpFileName;
        Pos++;

        Pos = ::GetDirectories(Directories, Pos, TmpFileName + Filter, Common_Flag);
      }
    }
    while (::FileFindNext(handle, FileName));

    ::FileFindClose(handle);
  }

  return(Pos);
}

void OnStart()
{
  string Folders[];
  
  GetDirectories(Folders);
  
  ArrayPrint(Folders);
}

Taken from here. Perhaps other functions will be useful there.

MQL5 Site / file.mqh - Скачать бесплатно скрипт 'ThirdPartyTicks' от 'fxsaber' для MetaTrader 5 в MQL5 Code Base
  • www.mql5.com
class FILE { private:   static 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 = FILE::GetDirectory(Filter...
 
Vladimir Karputov:

Then from the MQL5 documentation:

Thanks, but it's hard to apply yet...

 
fxsaber:

Taken from here. Perhaps the other functions there will be useful.

Thanks, the code works! And how to get only the directory names in the specified directory, without depth branching and file names?

 
Aleksey Vyazmikin:

Thank you, the code works! But how to get only the directory names in the specified directory, without depth branching and file names?

//        Pos = ::GetDirectories(Directories, Pos, TmpFileName + Filter, Common_Flag);
 
fxsaber:

Thanks, but I can't figure out how to get a directory in a specific subdirectory, i.e. I know that MQL5\Files has a "test" directory and I need to look at directories in it.

 
Aleksey Vyazmikin:

Thanks, but I can't figure out how to get a directory in a specific subdirectory, i.e. I know that MQL5\Files has a"test" directory and I need to look at directories in it.

  GetDirectories(Folders, 0, "test\\*");
 
fxsaber:

So I did, but the files are also listed there... how to distinguish files from folders is unclear.

Reason: