FileMove...

 

Hi all,

I am new to this programming language. I just can't find out WHY MT4 doesn't find the folder under the FileMove Function... I am missing something here...

The goal is to send an attachment to an email.. However the standard function SendMail doesn't provide that... So I want to upload it to Google Drive so I can use automated software to fetch new files and push them to my Telegram bot( as he does with my email now).

So if there is an easy way to send an attachment like a screenshot in the same email that would be great. For now I am just trying to move the image to my google drive folder...


So first I want to capture a screenshot:

----------------------------------------------------------------


   string date = TimeToString(TimeCurrent(),TIME_DATE);

   string hour = TimeHour(TimeCurrent());

   string minute = TimeMinute(TimeCurrent());

   string fileName = date + "." + hour + "." + minute +".gif";

   WindowScreenShot(fileName,1024,600,-1,-1,1);            //<<<--- fileName =   2020.15.05.21.59.gif

----------------------------------------------------------------

This file doesn't get stored in the common folder but here: "C:\\Users\\USERNAME\\AppData\\Roaming\\MetaQuotes\\Terminal\\WEIRDLOOKINGCODE\\MQL4\\Files"        <- Added double \\ else the path doesn't work at all...

So this is how the code looks like:

-----------------------------------------------

string InpSrcName= fileName;

string InpDstName= fileName;

string InpSrcDirectory="C:\\Users\\USERNAME\\AppData\\Roaming\\MetaQuotes\\Terminal\\WEIRDLOOKINGCODE\\MQL4\\Files";

string InpDstDirectory="C:\\Users\\USERNAME\\Google Drive\\Charts";

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

//| Script program start function                                    |

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

Sleep(5000);                                                                                                      //<<<--- Added a timer to make sure the file is created 

   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;

   Alert(src_path);                                                   //<<<--- When I alert the name and source of the file / directory it is correct.... It prints: C:\Users\USERNAME\AppData\Roaming\MetaQuotes\Terminal\WEIRDLOOKINGCODE\MQL4\Files\2020.15.05.21.59.gif

   

//--- check if the source file exists (if not - exit)

   if(FileIsExist(src_path))

      Alert("%s file exists in the %s\\Files\\%s folder",InpSrcName,local,InpSrcDirectory);

   else

     {

      Alert("Error, %s source file not found",InpSrcName);                                        //<<<--- Getting this error message saying file does not exist.... While my alert shows the EXACT path where the screenshots are being stored...

      return;

     }

//--- check if the result file already exists

   if(FileIsExist(dst_path,0))

     {

      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!");

-----------------------------------------------------------------------------------------------------------------------------------


Please help!

Documentation on MQL5: File Functions / FileMove
Documentation on MQL5: File Functions / FileMove
  • www.mql5.com
[in] Flag determining the location of the file. If common_flag = FILE_COMMON, then the file is located in a shared folder for all client terminals \Terminal\Common\Files. Otherwise, the file is located in a local folder ( [in] Access flags. The parameter can contain only 2 flags: FILE_REWRITE and/or FILE_COMMON - other flags are ignored. If the...
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Seantjuh1991: This file doesn't get stored in the common folder but here: "C:\\Users\\USERNAME\\AppData\\Roaming\\MetaQuotes\\Terminal\\WEIRDLOOKINGCODE\\MQL4\\Files"        <- Added double \\ else the path doesn't work at all...
    string src_path=InpSrcDirectory+"\\"+InpSrcName;
    
    string dst_path=InpDstDirectory+"\\"+InpDstName;
    FolderDelete using TERMINAL_DATA_PATH - Technical Analysis - General - MQL5 programming forum 2017.12.13
 
Please use code buttons to insert code. You can edit your post to fix this. The weird looking code is the id of your MT installation.
 
lippmaje:
Please use code buttons to insert code. You can edit your post to fix this. The weird looking code is the id of your MT installation.


 Yes I know that about the weird looking code :) 

 

MQL restricts file access to its sandbox only.

string InpDstDirectory="C:\\Users\\USERNAME\\Google Drive\\Charts";
string dst_path=InpDstDirectory+"\\"+InpDstName;
if(FileIsExist(dst_path,0))
    if(FileMove(src_path,0,dst_path,FILE_COMMON|FILE_REWRITE))

This won't work because it's beyond the sandbox.

 
lippmaje:

MQL restricts file access to its sandbox only.

This won't work because it's beyond the sandbox.


Thought so.... Will look for another solution then... There is always a way!

 

Either stick with the common folder or create a hardlink to escape the sandbox.

C:
cd C:\Users\USERNAME\AppData\Roaming\MetaQuotes\Terminal\Common\Files
mklink /D Google "C:\Users\USERNAME\Google Drive\Charts"
string InpDstDirectory="Google";
string dst_path=InpDstDirectory+"\\"+InpDstName;
if(FileIsExist(dst_path,0))
    if(FileMove(src_path,0,dst_path,FILE_COMMON|FILE_REWRITE))
 
lippmaje:

Either stick with the common folder or create a hardlink to escape the sandbox.

Using hardlink like that you could have some surprises.
 
Alain Verleyen:
Using hardlink like that you could have some surprises.


Thanks, I created a task in windows that moves the files every 5 minutes to the correct folder. It works now.


Third party software gets triggered when a new file gets added in that specific folder and fetches the last unread message from my email. And sends the text and screenshot from google drive to Telegram.