跳至内容

FileDelete

FileDelete

删除客户端终端本地文件夹中指定的文件。

bool  FileDelete(
   const string  file_name,     // File name to delete
   int           common_flag=0  // Location of the file to delete
   );

参数

file_name

[in] 文件名。

common_flag=0

[in] 标志用于确定文件位置。如果 common_flag = FILE_COMMON,则文件位于所有客户端终端的共享文件夹 \Terminal\Common\Files 中。否则,文件位于本地文件夹中。

返回值

如果失败,函数返回 false。

注意

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

从客户端终端的本地文件夹中删除指定的文件(测试时位于 MQL4\Files 或 MQL4\Tester\Files)。如果 common_flag = FILE_COMMON,则该函数将文件从所有客户端终端的共享文件夹中移除。

示例:

//--- show the window of input parameters when launching the script
#property script_show_inputs
//--- date for old files
input datetime InpFilesDate=D'2013.01.01 00:00';
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   string   file_name;      // variable for storing file names
   string   filter="*.txt"; // filter for searching the files
   datetime create_date;    // file creation date
   string   files[];        // list of file names
   int      def_size=25;    // array size by default
   int      size=0;         // number of files
//--- allocate memory for the array
   ArrayResize(files,def_size);
//--- receive the search handle in the local folder's root
   long search_handle=FileFindFirst(filter,file_name);
//--- check if FileFindFirst() executed successfully
   if(search_handle!=INVALID_HANDLE)
     {
      //--- searching files in the loop
      do
        {
         files[size]=file_name;
         //--- increase the array size
         size++;
         if(size==def_size)
           {
            def_size+=25;
            ArrayResize(files,def_size);
           }
         //--- reset the error value
         ResetLastError();
         //--- receive the file creation date
         create_date=(datetime)FileGetInteger(file_name,FILE_CREATE_DATE,false);
         //--- check if the file is old
         if(create_date<InpFilesDate)
           {
            PrintFormat("%s file deleted!",file_name);
            //--- delete the old file
            FileDelete(file_name);
           }
        }
      while(FileFindNext(search_handle,file_name));
      //--- close the search handle
      FileFindClose(search_handle);
     }
   else
     {
      Print("Files not found!");
      return;
     }
//--- check what files have remained
   PrintFormat("Results:");
   for(int i=0;i<size;i++)
     {
      if(FileIsExist(files[i]))
         PrintFormat("%s file exists!",files[i]);
      else
         PrintFormat("%s file deleted!",files[i]);
     }
  }
最后更新于