Download MetaTrader 5

FolderCreate

The function creates a folder in the Files directory (depending on the value of common_flag).

bool  FolderCreate(
   string  folder_name,       // String with the name of the new folder
   int     common_flag=0      // Scope
   );

Parameters

folder_name

[in] The name of the directory you want to create. Contains the full path to the folder.

common_flag=0

[in] Flag determining the location of the directory. If common_flag=FILE_COMMON, then the directory is in the shared folder for all client terminals \Terminal\Common\Files. Otherwise, the directory is in a local folder (MQL5\Files or MQL5\Tester\Files in case of testing).

Return Value

Returns true if successful, otherwise - false.

Note

For security reasons, work with files is strictly controlled in the MQL5 language. Files with which file operations are conducted using MQL5 means, cannot be outside the file sandbox.

Example:

//+------------------------------------------------------------------+
//|                                            Demo_FolderCreate.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//--- Description
#property description "The script shows a sample use of FolderCreate()."
#property description "An external parameter defines the folder for creating folders."
#property description "After running the script, a structure of folders is created"
 
//--- Show the dialog of input parameters when starting the script
#property script_show_inputs
//--- The input parameter defines the folder, in which the script is running
input bool     common_folder=false// A shared folder of all terminals
int            flag=0;              // The flag value determines the place for running file operations 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   string working_folder;
//--- Set the flag value, if the external parameter common_folder==true
   if(common_folder)
     {
      flag=FILE_COMMON;
      //--- Find the folder, in which we are working
      working_folder=TerminalInfoString(TERMINAL_COMMONDATA_PATH)+"\\MQL5\\Files";
     }
   else working_folder=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files";
//--- The folder that will be created in the folder MQL5\Files
   string root="Folder_A";
   if(CreateFolder(working_folder,root,flag))
     {
      //--- Create a child folder in it Child_Folder_B1
      string folder_B1="Child_Folder_B1";
      string path=root+"\\"+folder_B1;          // Create a folder name based on the structure
      if(CreateFolder(working_folder,path,flag))
        {
         //--- Create 3 more child folders in this folder
         string folder_C11="Child_Folder_C11";
         string child_path=path+"\\"+folder_C11;// Create a folder name based on the structure
         CreateFolder(working_folder,child_path,flag);
         //--- The second child folder
         string folder_C12="Child_Folder_C12";
         child_path=path+"\\"+folder_C12;
         CreateFolder(working_folder,child_path,flag);
 
         //--- The third child folder
         string folder_C13="Child_Folder_C13";
         child_path=path+"\\"+folder_C13;
         CreateFolder(working_folder,child_path,flag);
        }
     }
//---
  }
//+------------------------------------------------------------------+
//| Tries to create a folder and shows a message                     |
//+------------------------------------------------------------------+
bool CreateFolder(string working_folder,string folder_path,int file_flag)
  {
//--- A debug message   
   PrintFormat("folder_path=%s",folder_path);
//--- Trying to create a folder relative to path MQL5\Files
   if(FolderCreate(folder_path,file_flag))
     {
      //--- Show the entire path to the created folder
      PrintFormat("Folder %s has been created",working_folder+"\\"+folder_path);
      //--- Reset the error code
      ResetLastError();
      //--- Return successful result
      return true;
     }
   else
      PrintFormat("Failed to create folder %s. Error code %d",working_folder+folder_path,GetLastError());
//--- Failed
   return false;
  }

See also

FileOpen(), FolderClean(), FileCopy()


Updated: 2015.12.03