Problem with FileIsExist() function

 

Hi, I have a batch file that I want to check if it is exist or not in OnInit() function. I use this code to

if(FileIsExist("Taskkill.bat, 1")==false) 
      Print("***** file not exist, error code ", GetLastError(), " *****");
else
      Print("file exist");

The file is located in local data folder so I use '1' on the second parameter. Using this code the result is always false with error 5002. If I use file name without extension (Taskkill) it return error 5019 and both indicated that the file isn't found.

I then copy the batch file and paste it in the Common file folder but the result is the same with error 502 and 5019. How can I use this function correctly?

 
akbar giro: FileIsExist

If the file already exists in the Common folder, this should resolve the problem:

string  FileName = "Taskkill.bat";
bool fileExists= FileIsExist(FileName,FILE_COMMON);
if(fileExists) ...
Improperly formatted code edited by moderator.
 

Unfortunately it still return error 5002.

   string fileName = "Taskkill.bat";

   bool fileExist = FileIsExist(fileName);

   if(!fileExist)
      Print("file not exist error code ", GetLastError());
   else
      Print("file exist");
 
akbar giro #:

Unfortunately it still return error 5002.

I suggest to insert a "ResetLastError" before calling the file function.
EDIT:

Try checking the actual local path with TerminalInfoString.
https://www.mql5.com/en/docs/check/terminalinfostring
 
akbar giro #: Unfortunately it still return error 5002.

It appears that MetaTrader blocks files with the .bat or similar extensions, likely for security reasons.

I just renamed the file to .txt and also copied it to the ...\Common\Files subfolder. The code now returns true:

string fileName = "Taskkill.txt";
bool fileExists= FileIsExist(fileName , FILE_COMMON);

if(!fileExists)
   Print("file not exist error code ", GetLastError());
else
   Print("file exist");

Improperly formatted code edited by moderator.

 
@Alireza #:

Improperly formatted code edited by moderator.

Please, always use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Alireza #:

It appears that MetaTrader blocks files with the .bat or similar extensions, likely for security reasons.

I just renamed the file to .txt and also copied it to the ...\Common\Files subfolder. The code now returns true:

Improperly formatted code edited by moderator.

Yes you are correct, I change the extension and the function is working find. Will try to find a solution to this.

 
Fernando Carreiro #:

Please, always use the CODE button (Alt-S) when inserting code.

Sure , thank you 

Reason: