FolderClean

函数删除指定文件夹中所有文件。

bool  FolderClean(

   string  folder_name,       // 带有已删除的文件夹名的字符串

   int     common_flag=0      // 范围

   );

参量

folder_name

[in] 想要删除的所有文件的目录名称,包括文件夹的全部路径。

common_flag=0

[in] 标记 决定目录位置,如果common_flag=FILE_COMMON,那么目录在所有客户端的共享目录中 \Terminal\Common\Files,否则,目录会在本地文件夹中(MQL5\Files 或者 MQL5\Tester\Files )。

返回值

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

注释

出于安全考虑,工作文件必须严格由MQL5语言管理。使用MQL5实 施文件操作的文件意味着,不能在文件沙箱外。

该函数慎重使用,所有文件和子目录会被彻底删除。

示例 :

//+------------------------------------------------------------------+

//|                                             Demo_FolderClean.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"

//--- 描述

#property description "The script shows a sample use of FolderClean()."

#property description "First, files are created in the specified folder using the FileOpen() function."

#property description "Then, before the files are deleted, a warning is shown using MessageBox()."

 

//--- 启动脚本时显示输入参数对话框

#property script_show_inputs

//--- 输入参数

input string   foldername="demo_folder";  // 在MQL5/Files/创建一个文件夹

input int      files=5;                   // 要创建和删除的文件数量

//+------------------------------------------------------------------+

//| 脚本程序启动函数                                                   |

//+------------------------------------------------------------------+

void OnStart()

  {

   string name="testfile";

//--- 首先在程序端数据文件夹打开或创建文件

   for(int N=0;N<files;N++)

     {

      //--- 'demo_folder\testfileN.txt'格式的文件名称

      string filemane=StringFormat("%s\\%s%d.txt",foldername,name,N);

      //--- 打开用于编写的标识文件,在这种情况下将会自动创建'demo_folder'

      int handle=FileOpen(filemane,FILE_WRITE);

      //--- 找出FileOpen()函数是否成功

      if(handle==INVALID_HANDLE)

        {

         PrintFormat("Failed to create file %s. Error code",filemane,GetLastError());

         ResetLastError();

        }

      else

        {

         PrintFormat("File %s has been successfully opened",filemane);

         //--- 打开的文件不再需要,所以请关闭它

         FileClose(handle);

        }

     }



//--- 检查文件夹中的文件数量 

   int k=FilesInFolder(foldername+"\\*.*",0);

   PrintFormat("Totally the folder %s contains %d files",foldername,k);



//--- 显示询问用户的对话框

   int choice=MessageBox(StringFormat("You are going to delete %d files from folder %s. Do you want to continue?",foldername,k),

                         "Deleting files from the folder",

                         MB_YESNO|MB_ICONQUESTION); //  两个按钮 - "Yes" 和 "No"

   ResetLastError();



//--- 根据选定的变量运行一个行为

   if(choice==IDYES)

     {

      //--- 开始删除文件

      PrintFormat("Trying to delete all files from folder %s",foldername);

      if(FolderClean(foldername,0))

         PrintFormat("Files have been successfully deleted, %d files left in folder %s",

                     foldername,

                     FilesInFolder(foldername+"\\*.*",0));

      else

         PrintFormat("Failed to delete files from folder %s. Error code %d",foldername,GetLastError());

     }

   else

      PrintFormat("Deletion canceled");

//---

  }

//+------------------------------------------------------------------+

//| 在指定文件夹返回文件数量                                            |

//+------------------------------------------------------------------+

int FilesInFolder(string path,int flag)

  {

   int count=0;

   long handle;

   string filename;

//---

   handle=FileFindFirst(path,filename,flag);

//--- 如果至少一个文件被找到,请搜索更多的文件

   if(handle!=INVALID_HANDLE)

     {

      //--- 显示文件名称

      PrintFormat("File %s found",filename);

      //--- 增加找到的文件/文件夹的计数器

      count++;

      //--- 在所有文件/文件夹中开始搜索

      while(FileFindNext(handle,filename))

        {

         PrintFormat("File %s found",filename);

         count++;

        }

      //--- 完成时不要忘记关闭搜索句柄

      FileFindClose(handle);

     }

   else // 获得句柄失败

     {

      PrintFormat("Files search in folder %s failed",path);

     }

//--- 返回结果

   return count;

  }

另见

FileFindFirst, FileFindNext, FileFindClose