Hello,
CopyFileA or CopyFileW not working
any advice?
You have several errors.
You got error #4051 from mql5, but there is no error for mql5 and you don't use ResetLastError().
When you are using Winapi DLL call, you have to use WinApi GetLastError() to get the error.
So try this code :
#import "kernel32.dll" // bool CopyFileA(string lpExistingFileName,string lpNewFileName,bool bFailIfExists); bool CopyFileW(string lpExistingFileName,string lpNewFileName,bool bFailIfExists); int GetLastError(void); #import //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- //--- test1 CopyFileW string filename ="CopyFileW.txt"; string pathin = StringConcatenate(TerminalInfoString(TERMINAL_PATH),"\\MQL4\\Files\\",filename); string pathout = StringConcatenate(TerminalInfoString(TERMINAL_PATH),"\\MQL4\\",filename); Print(pathin); Print(pathout); int handle = FileOpen(filename,FILE_WRITE); // create file CopyFileW.txt if(handle!=-1) { ResetLastError(); if(!CopyFileW(pathin,pathout,false)) Print("CopyFileW error#",kernel32::GetLastError()," mql5 error#",::GetLastError()); //got error# 4051 } }There are still 2 errors, but with this code, you should be able to find them.
Hello,
CopyFileA or CopyFileW not working
any advice?
In your example you have to close the opened file handle before you can copy. Or rather, note the difference here in respect to the new build: " Shared use mode should be specified explicitly using FILE_SHARE_READ and/or FILE_SHARE_WRITE "
You have to close the opened file handle before you can copy.
Thanks to angevoyageur,2cent,gchrmt4,eliot
The correct codes as below:
//
#property copyright "Copyright 2013, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict #import "kernel32.dll" bool CopyFileW(string lpExistingFileName,string lpNewFileName,bool bFailIfExists); int GetLastError(void); #import //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- //--- test1 CopyFileW string filename ="CopyFileW.txt"; string pathin = StringConcatenate(TerminalInfoString(TERMINAL_DATA_PATH),"\\MQL4\\Files\\",filename); // instead of TERMINAL_PATH string pathout = StringConcatenate(TerminalInfoString(TERMINAL_DATA_PATH),"\\MQL4\\",filename); Print(pathin); Print(pathout); int handle = FileOpen(filename,FILE_WRITE); // create file CopyFileW.txt FileClose(handle); // before CopyFile, file shoud be closed . if(handle!=-1) { ResetLastError(); if(!CopyFileW(pathin,pathout,false)) Print("CopyFileW error#",kernel32::GetLastError()," mql5 error#",::GetLastError()); //got error# 4051 } } //+------------------------------------------------------------------+
//
also when create file with FILE_SHARE_READ:
int handle = FileOpen(filename,FILE_WRITE|FILE_SHARE_READ); // create file CopyFileW.txt with FILE_SHARE_READ if(handle!=-1) { ResetLastError(); if(!CopyFileW(pathin,pathout,false)) Print("CopyFileW error#",kernel32::GetLastError()," mql5 error#",::GetLastError()); //got error# 4051 FileClose(handle); // }
//
In your example you have to close the opened file handle before you can copy. Or rather, note the difference here in respect to the new build: " Shared use mode should be specified explicitly using FILE_SHARE_READ and/or FILE_SHARE_WRITE "
HI Guys,
I have been facing almost the same issue using the CopyFileA function : I used to copy a file from c:\1 to c:\2 folder (out ot TerminalPath)
Any idea how I can go through this?
#import "kernel32.dll" bool CopyFileA (string source_file, string destination_file, bool if_exist);//tava ok, entao mudei para o CopyFileW do sereguei #import string source="C:\\1\\file.txt"; string dest="C:\\2\\file.txt"; if(!CopyFileA(source, dest,true)==false) Print(" Copying error (source):",source," dest:",dest); else Print(" OK");
Already solved guys using all info above. thank you.
This worked great for me also for a while, but since recompiling under 610 I now get system error 32 - shared access violation.
Still works as a script but when I drop it into an EA function call it fails with error 32. Any ideas?
#import "kernel32.dll" bool CopyFileW(string lpExistingFileName,string lpNewFileName,bool bFailIfExists); int GetLastError(void); #import void DuplicateFile(string SourcePath, string TargetPath) { //--- string pathin = StringConcatenate(SourcePath,FileName); string pathout = StringConcatenate(TargetPath,FileName); int handle2 = FileOpen(FileName,FILE_CSV|FILE_WRITE,';'); FileClose(handle2); if(handle2!=-1) { ResetLastError(); if(!CopyFileW(pathin,pathout,false)) Print("CopyFileW error#",kernel32::GetLastError()," mql5 error#",::GetLastError()); //got error# 4051 } } string strSource = "C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\3BD2B5E5A5264AFE17C1E2DDC7D6B381\\MQL4\\Files\\"; DuplicateFile(strSource,"C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\F354DCCD667FB40D8D6DE5A4406D8652\\MQL4\\Files\\");
This worked great for me also for a while, but since recompiling under 610 I now get system error 32 - shared access violation.
Still works as a script but when I drop it into an EA function call it fails with error 32. Any ideas?
didn't you read what was written above ?
32 (0x20)
The process cannot access the file because it is being used by another process.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
CopyFileA or CopyFileW not working
any advice?