MQL4参考 文件函数 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



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


[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 (MQL4\Files or MQL4\Tester\Files in case of testing).

Return Value

Returns true if successful, otherwise - false.


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


//|                                            Demo_FolderCreate.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      ""
#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
      //--- Find the folder, in which we are working
   else working_folder=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL4\\Files";
//--- The folder that will be created in the folder MQL4\Files
   string root="Folder_A";
      //--- 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
         //--- 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
         //--- The second child folder
         string folder_C12="Child_Folder_C12";
         //--- The third child folder
         string folder_C13="Child_Folder_C13";
//| Tries to create a folder and shows a message                     |
bool CreateFolder(string working_folder,string folder_path,int file_flag)
//--- A debug message   
//--- Trying to create a folder relative to path MQL4\Files
      //--- Show the entire path to the created folder
      PrintFormat("Folder %s has been created",working_folder+"\\"+folder_path);
      //--- Reset the error code
      //--- Return successful result
      return true;
      PrintFormat("Failed to create folder %s. Error code %d",working_folder+folder_path,GetLastError());
//--- Failed
   return false;

See also

FileOpen(), FolderClean(), FileCopy()