Move/Copy file using DLL kernel32

 

I am trying to rename a file using kernel32 with following code:

//+------------------------------------------------------------------+
//|                                               JavascriptTest.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#import "kernel32.dll"
   int MoveFileA(string ExistingFilename, string NewFilename);
   int CopyFileA(string strExistingFile, string strCopyOfFile, int OverwriteIfCopyAlreadyExists);
   int GetLastError();
#import

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    const int handle2 = ::FileOpen("JavascriptFile.jsX", FILE_WRITE | FILE_TXT );

    if (handle2 != INVALID_HANDLE)
    {
      FileWrite(handle2, "var data = 123; ");
      FileClose(handle2);
    } else {
      Print ("Write Error ", GetLastError());
    }
    
    string FileFrom = "JavascriptFile.jsX";
    
    if (FileIsExist(FileFrom))
      Print (FileFrom, " Found!");
    else
      Print (FileFrom, " not Found");

    string PathFileFrom = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\" + FileFrom;
    string PathFileTo   = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\" + "JavascriptFile.jsY";

    Print (PathFileFrom);
    
    MoveFileA(PathFileFrom, PathFileTo);
    Print("MoveFileA error ",kernel32::GetLastError());

    CopyFileA(PathFileFrom, PathFileTo,0);
    Print("CopyFileA error ",kernel32::GetLastError());    
  }
//+------------------------------------------------------------------+

In my case the script returns the file being found, but both copy and move function return error code 2 (File not found), even though the file exists. If i copy the PathFileFrom path from the Experts Log and paste into Explorer, the file prompts. What could be the issue?

 
mql5 is using Unicode string, you would need to use the W version of the functions : MoveFileW, CopyFileW
 
Alain Verleyen:
mql5 is using Unicode string, you would need to use the W version of the functions : MoveFileW, CopyFileW

Thanks Alain,

Here's the working version:

//+------------------------------------------------------------------+
//|                                               JavascriptTest.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#import "kernel32.dll"
   int MoveFileW(string ExistingFilename, string NewFilename);
   int CopyFileW(string strExistingFile, string strCopyOfFile, int OverwriteIfCopyAlreadyExists);
   int GetLastError();
#import

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    const int handle2 = ::FileOpen("JavascriptFile.jsX", FILE_WRITE | FILE_TXT );

    if (handle2 != INVALID_HANDLE)
    {
      FileWrite(handle2, "var data = 123; ");
      FileClose(handle2);
    } else {
      Print ("Write Error ", GetLastError());
    }
    
    string FileFrom = "JavascriptFile.jsX";
    
    if (FileIsExist(FileFrom))
      Print (FileFrom, " Found!");
    else
      Print (FileFrom, " not Found");

    string PathFileFrom = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\" + FileFrom;
    string PathFileTo   = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\" + "JavascriptFile.js";

    Print (PathFileFrom);
    
    //MoveFileW(PathFileFrom, PathFileTo);
    //Print("MoveFileW error ",kernel32::GetLastError());

    CopyFileW(PathFileFrom, PathFileTo,0);
    Print("CopyFileW error ",kernel32::GetLastError());    
  }
//+------------------------------------------------------------------+
Reason: