跳至内容

FolderDelete

FolderDelete

此函数用于删除指定的目录。如果目录不为空,则无法删除。

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

参数

folder_name

[输入] 要删除的目录名称。包含到目录的完整路径。

common_flag=0

[输入] 用于确定目录位置的标志。标志决定了目录是否位于共享文件夹中。如果 common_flag=FILE_COMMON,则目录位于所有客户端终端的共享文件夹 \Terminal\Common\Files 中。否则,目录位于本地文件夹中(测试情况下为 MQL4\Files 或 MQL4\Tester\Files)。

返回值

如果成功则返回 true,否则返回 false。

注意

出于安全考虑,在 MQL4 语言中处理文件受到严格控制。使用 MQL4 进行文件操作的文件不能位于文件沙箱之外。

如果目录至少包含一个文件和/或子目录,则无法删除该目录,必须先清空它。FolderClean() 用于清空目录中的所有文件或子目录。

示例:

//+------------------------------------------------------------------+
//|                                            Demo_FolderDelete.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//--- Description
#property description "The script shows a sample use of FolderDelete()."
#property description "First two folders are created; one of them is empty, the second one contains a file."
#property description "When trying to delete a non-empty folder, an error is returned and a warning is shown."

//--- Show the dialog of input parameters when starting the script
#property script_show_inputs
//--- Input parameters
input string   firstFolder="empty";    // An empty folder
input string   secondFolder="nonempty";// The folder, in which one file will be created
string filename="delete_me.txt";       // The name of the file that will be created in folder secondFolder
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- Write the file handle here
   int handle;
//--- Find out in what folder we are working
   string working_folder=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL4\\Files";
//--- A debug message
   PrintFormat("working_folder=%s",working_folder);
//--- Trying to create an empty folder relative to path MQL4\Files
   if(FolderCreate(firstFolder,0)) // 0 means that we are working in the local folder of the terminal
     {
      //--- Enter the full path to the created folder
      PrintFormat("Folder %s has been created",working_folder+"\\"+firstFolder);
      //--- Reset the error code
      ResetLastError();
     }
   else
      PrintFormat("Failed to create folder %s. Error code %d",working_folder+"\\"+firstFolder, GetLastError());

//--- Now create a non-empty folder using the FileOpen() function
   string filepath=secondFolder+"\\"+filename;  // Form path to file that we want to open to write in a nonexistent folder
   handle=FileOpen(filepath,FILE_WRITE|FILE_TXT); // Flag FILE_WRITE in this case is obligatory, see Help for FileOpen
   if(handle!=INVALID_HANDLE)
      PrintFormat("File %s has been opened for reading",working_folder+"\\"+filepath);
   else
      PrintFormat("Failed to create file %s in folder %s. Error code=",filename,secondFolder, GetLastError());

   Comment(StringFormat("Prepare to delete folders %s and %s", firstFolder, secondFolder));
//--- A small pause of 5 seconds to read a message in the chart
   Sleep(5000); // Sleep() cannot be used in indicators!

//--- Show a dialog and ask the user
   int choice=MessageBox(StringFormat("Do you want to delete folders %s and %s?", firstFolder, secondFolder),
                         "Deleting folders",
                         MB_YESNO|MB_ICONQUESTION); //  Two buttons - "Yes" and "No"

//--- Run an action depending on the selected variant
   if(choice==IDYES)
     {
      //--- Delete the comment form the chart
      Comment("");
      //--- Add a message into the "Experts" journal
      PrintFormat("Trying to delete folders %s and %s",firstFolder, secondFolder);
      ResetLastError();
      //--- Delete the empty folder
      if(FolderDelete(firstFolder))
         //--- The following message should appear since the folder is empty
         PrintFormat("Folder %s has been successfully deleted",firstFolder);
      else
         PrintFormat("Failed to delete folder %s. Error code=%d", firstFolder, GetLastError());

      ResetLastError();
      //--- Delete the folder that contains a file
      if(FolderDelete(secondFolder))
         PrintFormat("Folder %s has been successfully deleted", secondFolder);
      else
         //--- The following message should appear since the folder contains a file
         PrintFormat("Failed to delete folder %s. Error code=%d", secondFolder, GetLastError());
     }
   else
      Print("Deletion canceled");
//---
  }

另请参阅

FileOpen(), FolderClean(), FileMove()

最后更新于