跳至内容

FileMove

FileMove

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

bool  FileMove(
   const string  src_file_name,    // File name for the move operation
   int           common_flag,      // Location
   const string  dst_file_name,    // Name of the destination file
   int           mode_flags        // Access mode
   );

参数

src_file_name

[输入] 要移动/重命名的文件名。

common_flag

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

dst_file_name

[输入] 操作后的文件名

mode_flags

[输入] 访问标志。该参数只能包含 2 个标志:FILE_REWRITE 和/or FILE_COMMON - 其他标志将被忽略。如果文件已存在且未指定 FILE_REWRITE 标志,则不会重写文件,函数将返回 false。

返回值

如果失败,函数将返回 false。

注意

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

如果新文件已存在,复制操作将根据 modeFlags 参数中 FILE_REWRITE 标志的存在情况进行执行。

示例:

//--- display the window of input parameters when launching the script
#property script_show_inputs
//--- input parameters
input string InpSrcName="data.txt";
input string InpDstName="newdata.txt";
input string InpSrcDirectory="SomeFolder";
input string InpDstDirectory="OtherFolder";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   string local=TerminalInfoString(TERMINAL_DATA_PATH);
   string common=TerminalInfoString(TERMINAL_COMMONDATA_PATH);
//--- get file paths
   string src_path=InpSrcDirectory+"//"+InpSrcName;
   string dst_path=InpDstDirectory+"//"+InpDstName;
//--- check if the source file exists (if not - exit)
   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;
     }
//--- check if the result file already exists
   if(FileIsExist(dst_path,FILE_COMMON))
     {
      PrintFormat("%s file exists in the %s\\Files\\%s folder",InpDstName,common,InpDstDirectory);
      //--- file exists, moving should be performed with FILE_REWRITE flag
      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);
      //--- the file does not exist, moving should be performed without FILE_REWRITE flag
      ResetLastError();
      if(FileMove(src_path,0,dst_path,FILE_COMMON))
         PrintFormat("%s file moved",InpSrcName);
      else
         PrintFormat("Error! Code = %d",GetLastError());
     }
//--- the file is moved; let's check it out
   if(FileIsExist(dst_path,FILE_COMMON) && !FileIsExist(src_path,0))
      Print("Success!");
   else
      Print("Error!");
  }

另请参阅

FileIsExist()

最后更新于