MQL4参考 文件函数 FolderDelete


The function removes the specified directory. If the folder is not empty, then it can't be removed.

bool  FolderDelete(
   string  folder_name,       // String with the name of the folder to delete
   int     common_flag=0      // Scope



[in] The name of the directory you want to delete. 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 the 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.

If the directory contains at least one file and/or subdirectory, then this directory can't be deleted, it must be cleared first. FolderClean() is used to clear a folder of all its files or subfolders.


//|                                            Demo_FolderDelete.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 FolderCreate() application sample."
#property description "The external parameter defines the directory for creating folders."
#property description "The folder structure is created after executing the script"
//--- display window of the input parameters during the script's launch
#property script_show_inputs
//--- the input parameter defines the folder, in which the script works
input bool     common_folder=false// common folder for all terminals
//| Script program start function                                    |
void OnStart()
//--- folder to be created in MQL5\Files
   string root_folder="Folder_A";
      //--- create the Child_Folder_B1 sub-folder in it
      string folder_B1="Child_Folder_B1";
      string path=root_folder+"\\"+folder_B1;          // create the folder name considering the structure
         //--- create 3 more sub-directories in this folder
         string folder_C11="Child_Folder_C11";
         string child_path=root_folder+"\\"+folder_C11;// create the folder name considering the structure
         //--- second sub-directory
         string folder_C12="Child_Folder_C12";
         //--- third sub-directory
         string folder_C13="Child_Folder_C13";
//| Try creating a folder and display a message about that           |
bool CreateFolder(string folder_path,bool common_flag)
   int flag=common_flag?FILE_COMMON:0;
   string working_folder;
//--- define the full path depending on the common_flag parameter
//--- debugging message  
//--- attempt to create a folder relative to the MQL5\Files path
      //--- display the full path for the created folder
      PrintFormat("Created the folder %s",working_folder+"\\"+folder_path);
      //--- reset the error code
      //--- successful execution
      return true;
      PrintFormat("Failed to create the folder %s. Error code %d",working_folder+folder_path,GetLastError());
//--- execution failed
   return false;

See also

FileOpen(), FolderClean(), FileMove()