Смотри, как бесплатно скачать роботов
Ищи нас в Telegram!
Ставь лайки и следи за новостями
Интересный скрипт?
Поставь на него ссылку - пусть другие тоже оценят
Понравился скрипт?
Оцени его работу в терминале MetaTrader 5
Библиотеки

CDir (MT5) - класс для получения оглавления каталога - библиотека для MetaTrader 5

Просмотров:
1662
Рейтинг:
(20)
Опубликован:
2017.03.07 16:38
\MQL5\Include\WIN_API\ \MQL5\Scripts\
Нужен робот или индикатор на основе этого кода? Закажите его на бирже фрилансеров Перейти на биржу

Иногда возникает необходимость выйти за пределы "песочницы" и прочитать оглавление, проверить наличие файла или каталога в файловой системе. Также может потребоваться узнать атрибуты файла или директории, размер файла, время его создания, последнего доступа к нему или записи. Пример возможного решения таких задач представлен этим кодом:

Стандартный пролог и описание переменных, а также ссылка на исходный код включаемого класса #include <WIN_API\Dir_API.mqh> - явное указание компилятору, что ему следует искать этот файл в директории \MQL5\Include\WIN_API. Вы вольны изменить эту ссылку в зависимости от ваших предпочтений в организации работы с включаемыми файлами руководствуясь документацией.

//+------------------------------------------------------------------+
//|                                            ExampleDirClass.mq4/5 |
//|                                        Copyright © 2017, Avatara |
//|                            https://www.mql5.com/en/users/avatara |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2017, Avatara"            2017/02/12
#property link      "https://www.mql5.com/en/users/avatara"
#property description "-- Example Dir Class --------------"
#property strict
#include <WIN_API\Dir_API.mqh> 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   File_Def File;            // work structure
   CDir Dir; int total,i;     
   string path = TerminalInfoString(TERMINAL_COMMONDATA_PATH)+
               "\\..\\..\\Terminal";
//---    
  

Как мы видим, строковая переменная path содержит путь к папке, находящейся на два уровня выше стандартного расположения каталога запущенного терминала, и мы (в случае если не использовалась опция /portable) получим путь к каталогу, содержащего пользовательские данные всех клиентских терминалов этого пользователя.

Вдумчивый читатель скажет, что мы делаем лишние телодвижения - достаточно подняться на один уровень и мы будем в нужном каталоге. Да, но а если мы захотим осуществить навигацию к другому каталогу - например: Crashes или Tester? Для нашего примера, это поучительней.

Вернемся к коду примера.

/---    
   total = Dir.Create(path);       // init dir
   Print (path," Total=",total-3); //Common,Help && Community not calc
    for (i = 0;i<total;i++)
    {
     string nameFile = Dir.GetNameFile(i);
     if(nameFile == "Common"||nameFile=="Help"||nameFile=="Community")
                                                              continue;
     File = Dir.GetStruct(i);
     Print (StringFormat ("%37s \t%8X\t  %5s\t",nameFile,
     File.FileAttributes,File.isDir?"-SubDir-":""),
     File.isDir?"            ":IntegerToString(File.FileLength,12),
     File.CreationTime,"     ",File.LastWriteTime,"     ",
     File.LastAccessTime);
    }
    Dir.Clear(); 
//---    

Создав экземпляр класса, мы получим в переменной total количество элементов (файлов или поддиректорий). Организовав цикл, напечатаем список каталогов терминалов (пропуская общие директории). Доступ к данным экземпляра мы иллюстрируем доступом к копии рабочей структуры данных класса File.

  path=TerminalInfoString(TERMINAL_PATH)+
              "\\..";
    Dir.Create(path,"\\M*");   // use filter "M*"       
    Print ("Alternative access:");
    total = Dir.Size(); 
    Print (Dir.Path," Total=",total);
    for (i = 0;i<total;i++)
    {
    Print  (StringFormat("%57s \t%8X \t %s \t",Dir.GetNameFile(i), 
                         Dir.GetStruct(i).FileAttributes,
                         Dir.isDir(i)?"--- SubDir ---": 
                         IntegerToString(Dir.GetFileLength(i),14)),
    "\t   Last modify: ",Dir.GetLastWriteTime(i));
    }
//---    
    Dir.Clear();  

На примере распечатки части оглавления директории (используется селекции элементов, содержащих в начале названия букву M) демонстрируются другие методы работы с элементами класса.

И в продолжение, часть кода, где мы проверяем наличие конкретного файла и узнаем его длину.

//---  Use to check the file
    path="C:";
    Print("------------ Use to check the file ---------------------");
    stringfilter="\\pagefile.sys";
    Dir.Create(path,filter);
    total=Dir.Size();
    PrintFormat("Find %d entry.",total);
    if(total>0)
      for(i =0;i<total;i++)Print("File search \"",path,filter,
        "\" is return:",Dir.isDir(i)?"--- SubDir ---  "+
        Dir.GetNameFile(i):Dir.GetNameFile(i)+
        IntegerToString(Dir.GetFileLength(i),14)+" byte. \t",
        StringFormat("Atributes=%X",Dir.GetAtributes(i)));
    else  Print("File ",path,filter," is absent.");
//---
    Dir.Clear();              

Завершаем тестовый скрипт примером использования ссылки на экземпляр класса хранения элемента оглавления директории:

  //--- use pointer to access data

   path="C:\\Temp";

    //---------------------> pointer declarate     CDir_API *e;        Print("------ Pointer use example. --------");     total =Dir.Create(path);     PrintFormat("%s       Find %d entry.",path,total);     if(total>0)       for(i =0;i<total;i++)       {       e=Dir.GetPointers(i);       Print(StringFormat("%77s \t%8X\t  %5s\t ",e.Name_File,           e.FileDef.FileAttributes,e.FileDef.isDir?"-SubDir-":""),           e.FileDef.isDir?"         ":           IntegerToString(e.FileDef.FileLength,12),"   ",           e.FileDef.CreationTime,"     ",           e.FileDef.LastWriteTime,"     ",           e.FileDef.LastAccessTime);       }       else  Print("File ",path,filter," is absent."); //---     Dir.Clear();     Print("------------- The end. -----------------------"); } //+------------------------------------------------------------------+

Не сомневаюсь, что большинство читателей воспользуются именно этим способом доступа к данным класса в своей работе.

Для расшифровки атрибутов может потребоваться дополнительные сведения:

FILE_ATTRIBUTE_ARCHIVE 32 (0x20) A file or directory that is an archive file or directory. Applications typically use this attribute to mark files for backup or removal.
FILE_ATTRIBUTE_COMPRESSED 2048 (0x800) A file or directory that is compressed. For a file, all of the data in the file is compressed. For a directory, compression is the default for newly created files and subdirectories.
FILE_ATTRIBUTE_DEVICE 64 (0x40) This value is reserved for system use.
FILE_ATTRIBUTE_DIRECTORY 16 (0x10) The handle that identifies a directory.
FILE_ATTRIBUTE_ENCRYPTED 16384 (0x4000) A file or directory that is encrypted. For a file, all data streams in the file are encrypted. For a directory, encryption is the default for newly created files and subdirectories.
FILE_ATTRIBUTE_HIDDEN 2 (0x2) The file or directory is hidden. It is not included in an ordinary directory listing.
FILE_ATTRIBUTE_INTEGRITY_STREAM 32768 (0x8000)

The directory or user data stream is configured with integrity (only supported on ReFS volumes). It is not included in an ordinary directory listing. The integrity setting persists with the file if it's renamed. If a file is copied the destination file will have integrity set if either the source file or destination directory have integrity set.

Windows Server 2008 R2, Windows 7, Windows Server 2008, Windows Vista, Windows Server 2003 and Windows XP: This flag is not supported until Windows Server 2012.

FILE_ATTRIBUTE_NORMAL 128 (0x80) A file that does not have other attributes set. This attribute is valid only when used alone.
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 8192 (0x2000) The file or directory is not to be indexed by the content indexing service.
FILE_ATTRIBUTE_NO_SCRUB_DATA 131072 (0x20000)

The user data stream not to be read by the background data integrity scanner (AKA scrubber). When set on a directory it only provides inheritance. This flag is only supported on Storage Spaces and ReFS volumes. It is not included in an ordinary directory listing.

Windows Server 2008 R2, Windows 7, Windows Server 2008, Windows Vista, Windows Server 2003 and Windows XP: This flag is not supported until Windows 8 and Windows Server 2012.

FILE_ATTRIBUTE_OFFLINE 4096 (0x1000) The data of a file is not available immediately. This attribute indicates that the file data is physically moved to offline storage. This attribute is used by Remote Storage, which is the hierarchical storage management software. Applications should not arbitrarily change this attribute.
FILE_ATTRIBUTE_READONLY 1 (0x1) A file that is read-only. Applications can read the file, but cannot write to it or delete it. This attribute is not honored on directories. For more information, see You cannot view or change the Read-only or the System attributes of folders in Windows Server 2003, in Windows XP, in Windows Vista or in Windows 7.
FILE_ATTRIBUTE_REPARSE_POINT 1024 (0x400) A file or directory that has an associated reparse point, or a file that is a symbolic link.
FILE_ATTRIBUTE_SPARSE_FILE 512 (0x200) A file that is a sparse file.
FILE_ATTRIBUTE_SYSTEM 4 (0x4) A file or directory that the operating system uses a part of, or uses exclusively.
FILE_ATTRIBUTE_TEMPORARY 256 (0x100) A file that is being used for temporary storage. File systems avoid writing data back to mass storage if sufficient cache memory is available, because typically, an application deletes a temporary file after the handle is closed. In that scenario, the system can entirely avoid writing the data. Otherwise, the data is written after the handle is closed.
FILE_ATTRIBUTE_VIRTUAL 65536 (0x10000) This value is reserved for system use.

Надеюсь, что этот пример, как и сам класс будет вам полезен, а я не сильно утомил своими пояснениями... ;)

Наслаждайтесь!

ColorXCCI_Histogram_HTF ColorXCCI_Histogram_HTF

Индикатор ColorXCCI_Histogram с возможностью изменения таймфрейма индикатора во входных параметрах.

VTS VTS

Трендовый индикатор.

Exp_IBS_RSI_CCI_v4_X2 Exp_IBS_RSI_CCI_v4_X2

Трендовая торговая система Exp_IBS_RSI_CCI_v4_X2 на основе сигналов двух индикаторов IBS_RSI_CCI_v4

Steve Cartwright Trader Camel CCI MACD Steve Cartwright Trader Camel CCI MACD

Используются две МA, одна MACD и одна CCI. OnTradeTransaction используется для получения времени открытия позиции.