CopyFileW()/MoveFileW()/FileMove() problems..

 

Hello guys,

I need some help with File-Management...

if some requirements are met, my indicator saves a screenshot. This screenshot can only be saved in MQL5/Files. Now I want to copy or move this screenshot to any free folder.

I tried it with CopyFileW() and MoveFileW() but it didn't work properly. I also tried it with the built-in FileMove() function but I am too stupid for that. I just don't understand

where and how FileMove() exactly moves files.

Can someone please tell me how to move a file from the standard MQ5/Files to any other folder?

I need this because I want to synchronize this folder with Google Drive and I can't use the standard MQL5/Files folder.

Thanks a lot!!


I tried it this way but I don't really know what I am doing here..

FileMove(TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files\\USDJPY.png", 0,
TerminalInfoString(TERMINAL_COMMONDATA_PATH)+"\\Files\\USDJPY.png", FILE_COMMON);
Documentation on MQL5: File Functions / FileMove
Documentation on MQL5: File Functions / FileMove
  • www.mql5.com
FileMove - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
#import"kernel32.dll"
        int CopyFileW(string, string, int);
#import

input string    File_name = "File";     //File Name

:
:
:

string src = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\" + File_name + ".png";
string dst = "C:\\Users\\xxxx\\Documents\\PNG_File\\" + File_name + ".png";
   
if (CopyFileW(src, dst, 0) == 1)
   Alert("Files have been copied.");

Try this one.

Maybe you'll find the file in your "Documents\PNG_File".

 
Nagisa Unada:

Try this one.

Maybe you'll find the file in your "Documents\PNG_File".

I coded this indicator to make some tests. I have no idea why it doesn't work although everything seems to be alright. The screenshots can only be found in MQL5/Files.

#property indicator_chart_window
#import "kernel32.dll"  //http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx
   bool CopyFileW (string lpExistingFileName, string lpNewFileName, bool failIfExists); //false=overwrite, true don't overwrite existing file
   bool MoveFileW (string lpExistingFileName, string lpNewFileName);
   bool DeleteFileW(string lpExistingFileName);
#import

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

   static datetime time0;
   if (time0==iTime(_Symbol, PERIOD_M1, 0)) return(rates_total);
   time0=iTime(_Symbol, PERIOD_M1, 0);
   string filename=_Symbol+(string)(long)TimeCurrent()+".png";
   ChartScreenShot(0, filename, 1280, 720);
   CopyFileW(TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files"+filename, "D:\\"+filename, false);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Marbo:

I coded this indicator to make some tests. I have no idea why it doesn't work although everything seems to be alright. The screenshots can only be found in MQL5/Files.

Your import DLL description will not work.

The file is created the first time OnCalculate is run, and is copied the second time it is run.

Therefore, it will not be copied if the following lines are present.

 static datetime time0;
 if (time0 == iTime(_Symbol, PERIOD_M1, 0)) return(rates_total);
 time0 = iTime(_Symbol, PERIOD_M1, 0);

Try this one.

#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0
/*
#import "kernel32.dll"  //http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx
   bool CopyFileW (string lpExistingFileName, string lpNewFileName, bool failIfExists); //false=overwrite, true don't overwrite existing file
   bool MoveFileW (string lpExistingFileName, string lpNewFileName);
   bool DeleteFileW(string lpExistingFileName);
#import
*/
#import"kernel32.dll"
        int CopyFileW(string, string, int);
#import
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping

   //---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   //---
   /*static datetime time0;
   if (time0 == iTime(_Symbol, PERIOD_M1, 0)) return(rates_total);
   time0 = iTime(_Symbol, PERIOD_M1, 0);*/

   string filename = _Symbol + (string)(long)TimeCurrent() + ".png";
   ChartScreenShot(0, filename, 1280, 720);

   string src = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\" + filename;
   string dst = "D:\\" + filename;

   if (CopyFileW(src, dst, 0) > 0)
      Alert("Files have been copied.");
   //--- return value of prev_calculated for next call
   return(rates_total);
}
 
Nagisa Unada:

Your import DLL description will not work.

The file is created the first time OnCalculate is run, and is copied the second time it is run.

Therefore, it will not be copied if the following lines are present.

Try this one.

You are right. It works perfect. Thank you very much for that!!

Could you please tell me why it doesn't work in one loop? The loop contains the screenshot and the copy process. I don't understand why this doesn't work one after the other.

 
Marbo:

You are right. It works perfect. Thank you very much for that!!

Could you please tell me why it doesn't work in one loop? The loop contains the screenshot and the copy process. I don't understand why this doesn't work one after the other.

I think I know the problem. The screenshot takes a while until it is saved. And during this save-process the CopyFileW() doesn't find the file.

I changed it this way and now it works perfect:

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   //---
   
   static datetime time0;
   if (time0!=iTime(_Symbol, PERIOD_M1, 0)) {
      filename = _Symbol + (string)(long)TimeCurrent() + ".png";
      ChartScreenShot(0, filename, 1280, 720);
      time0=iTime(_Symbol, PERIOD_M1, 0);
   }
   if (FileIsExist(filename)) {
      string src = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\" + filename;
      string dst = "D:\\" + filename;  
      CopyFileW(src, dst, 0);
   }

   //--- return value of prev_calculated for next call
   return(rates_total);
}
 
Marbo:

I think I know the problem. The screenshot takes a while until it is saved. And during this save-process the CopyFileW() doesn't find the file.

I changed it this way and now it works perfect:

That's right.

Congrats on completing it!

 
Nagisa Unada:

That's right.

Congrats on completing it!

Thank you for your help! 

Reason: