Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

CDir (MT5) - Class for getting directory contents - MetaTrader 5 Library - MetaTrader 5 Resources

author EAcpu | 3 reads | 0 comments |

CDir (MT5) - a class for getting directory contents - library for MetaTrader 5

CDir (MT5) - a class for getting directory contents - library for MetaTrader 5

Sometimes it is necessary to go beyond the sandbox and read the contents to check whether a file or directory exists in the file system. Additionally, you may need to find the file or directory's attributes, file size, creation time, and last access or write. The following code shows an example of how to solve these problems:

Standard prologue and variable descriptions and #include The link containing the source code of the class is an implicit instruction to the compiler that it should look for files in the folder \MQL5\Include\WIN_API. You are free to change this link according to your preferences in the work organization containing the file. document .

 //+------------------------------------------------------------------+
//| Example DirClass.mq4/5 |
//| Copyright © 2017, Avatar |
//| https://www.mql5.com/en/users/avatara |
//+------------------------------------------------------------------+
#Property Copyright " Copyright © 2017, Avatar " 2017/02/12
#Property association "https://www.mql5.com/en/users/avatara"
#Property Description "--Directory Class Example--------------"
#propertystrict
#include //+------------------------------------------------------------------+
//|Script program startup function |
//+------------------------------------------------------------------+
blank start ()
  {
  File_Def file; // working structure Cdir directory; integer total, i;  
string path = terminal information string ( TERMINAL_COMMONDATA_PATH ) +
"\\..\\..\\Terminal" ; //---

We can see that the string variable path contains the path to a folder that is two levels above the standard location of the running terminal directory, and we (if the /portable option is not used) get the path to the directory that contains the user data for all clients for that user.

Thoughtful readers will say that we made an extra move - in order to get into the correct directory, it was enough to go up a level. But what if we want to navigate to another folder (such as Crashes or Tester)? Here is a more instructive example.

CDir (MT5) - a class for getting directory contents - library for MetaTrader 5

Let's get back to the sample code.

/--- 
     total = directory.create(path) ; // initialize directory print (path, "total=" , all - 3 ); //Common,Help && communities are not counted for (i= 0 ;i<total;i++)
    {
StringNameFile = Directory.GetNameFile(i) ;
if (namefile == "common" || namefile == "help" || namefile == "community" )
continue ;
File = Directory.GetStruct(i) ;
Print ( string format ( "%37s \t%8X\t %5s\t" , name file,
File.FileAttributes , File.isDir ? "-subdirectory-" : "" ),
File.isDir ? " " : Integer to string ( file.file length , 12 ),
File.CreationTime , " " , File.LastWriteTime , " " ,
file.last access time );
    }
Directory.Clear() ; //---

After creating a class instance, we receive the total number of elements (files and subdirectories) in the class. By organizing the loop, we can print the terminal directory listing (skipping shared directories). Let us illustrate access to instance data by accessing a copy of the working data structure of the file class.

 Path = terminal information string ( terminal path ) +
"\\.." ;
directory.Create(path, "\\M*" ); // Use filter "M*" to print ( "Alternate Access:" );
    total = directory size(); print ( directory path , "total=" , all);
for (i= 0 ;i<total;i++)
    {
Print ( string format ( "%57s \t%8X \t %s \t" , directory.GetNameFile(i) , 
Dir.GetStruct(i).FileAttributes ,
Directory.isDir(i) ? "---Subdirectory---" : 
Integer to string ( catalog.GetFileLength(i) , 14 )),
"\tLast modified:" , directory.GetLastWriteTime(i) );
    } //--- Directory.Clear() ;

The example of printing part of the contents of a directory (using select elements starting with the letter M) demonstrates other ways of working with class members.

In the code below, we check if a specific file exists and find out its length.

//--- used to check file path = "C:" ;
print ( "----------------for checking files---------------------" );
string filter = "\\pagefile.sys" ;
Dir.Create(path, filter); total = directory size() ;
printFormat ( "Found %d entries." , all);
if (total > 0 )
for (i= 0 ;i<total;i++) print ( "File Search\"" ,path,filter,
"\" is a carriage return: " , directory.isDir(i) ? "--- subdirectory--- " +
Directory.GetNameFile(i) : Directory.GetNameFile(i) +
Integer to string ( directory.GetFileLength(i) , 14 )+ "Bytes.\t" ,
String format ( "Attribute=%X" , directory.GetAtributes(i) ));
Other print ( "File" , path, filter, "Absent." ); //--- Directory.Clear();

The test script ends with an example using a reference to a class instance that stores the directory content element:

//--- use pointers to access data

path = "C:\\Temperature" ;

I have no doubt that most readers will use this method to access class data at work.

Additional information may be needed to understand the properties:

Directory or user data flow configured for integrity (only supported on ReFS volumes). It is not included in normal directory listings. If renamed, the integrity settings will remain in the file. If you copy a file, if the source file or destination directory has integrity set, the destination file will 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 before Windows Server 2012.

User data streams are not read by background data integrity scanners (also known as sanitizers). When set on a directory, it only provides inheritance. This flag is only supported for Storage Spaces and ReFS volumes. It is not included in normal directory listings.

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

I hope this example and the course itself are useful to you.


Attachment download

📎 dir_api.mqh (16.64 KB)

📎 exampledirclass.mq5 (7.66 KB)

Source: MQL5 #17623

Verification code Refresh