FileMove

从本地或共享文件夹中移动文件到另一文件夹。

bool  FileMove(
   const string  src_file_name,    // 进行移动操作的文件名
   int           common_flag,      // 位置
   const string  dst_file_name,    // 目标文件名
   int           mode_flags        // 访问模式
   );

参量

src_file_name

[in]  移动/重命名的文件名称。

common_flag

[in] 标记 决定文件位置,如果common_flag = FILE_COMMON,文件为所有客户端放置共享文件夹 \Terminal\Common\Files。否则,文件放置在本地文件夹中。

dst_file_name

[in]  操作后的文件名称。

mode_flags

[in] 访问标记。 参量只包括2个标签:FILE_REWRITE 和/或者 FILE_COMMON -忽略另外标签。如果文件已经存在并且FILE_REWRITE 标签未指定,文件不会再编辑,函数返回错误值。

返回值

如果失败,函数返回错误值。

注释

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

如果新文件已经存在,复制文件在mode_flags 参量中取决于FILE_REWRITE标签的可用性。

示例:

//--- 启动脚本时显示输入参数的窗口
#property script_show_inputs
//--- 输入参数
input string InpSrcName="data.txt";
input string InpDstName="newdata.txt";
input string InpSrcDirectory="SomeFolder";
input string InpDstDirectory="OtherFolder";
//+------------------------------------------------------------------+
//| 脚本程序启动函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
   string local=TerminalInfoString(TERMINAL_DATA_PATH);
   string common=TerminalInfoString(TERMINAL_COMMONDATA_PATH);
//--- 接收文件路径
   string src_path;
   string dst_path;
   StringConcatenate(src_path,InpSrcDirectory,"//",InpSrcName);
   StringConcatenate(dst_path,InpDstDirectory,"//",InpDstName);
//--- 检查源文件是否存在(如果不 - 退出)
   if(FileIsExist(src_path))
      PrintFormat("%s file exists in the %s\\Files\\%s folder",InpSrcName,local,InpSrcDirectory);
   else
     {
      PrintFormat("Error, %s source file not found",InpSrcName);
      return;
     }
//--- 检查结果文件是否已经存在
   if(FileIsExist(dst_path,FILE_COMMON))
     {
      PrintFormat("%s file exists in the %s\\Files\\%s folder",InpDstName,common,InpDstDirectory);
      //--- 如果文件存在,移动应该通过FILE_REWRITE标识来完成
      ResetLastError();
      if(FileMove(src_path,0,dst_path,FILE_COMMON|FILE_REWRITE))
         PrintFormat("%s file moved",InpSrcName);
      else
         PrintFormat("Error! Code = %d",GetLastError());
     }
   else
     {
      PrintFormat("%s file does not exist in the %s\\Files\\%s folder",InpDstName,common,InpDstDirectory);
      //--- 如果文件不存在,移动应该无需通过FILE_REWRITE标识来完成
      ResetLastError();
      if(FileMove(src_path,0,dst_path,FILE_COMMON))
         PrintFormat("%s file moved",InpSrcName);
      else
         PrintFormat("Error! Code = %d",GetLastError());
     }
//--- 文件被移动;我们去看看
   if(FileIsExist(dst_path,FILE_COMMON) && !FileIsExist(src_path,0))
      Print("Success!");
   else
      Print("Error!");
  }

另见

FileIsExist