FileIsExist always defaults to File exists already! what am i missing?

 

I am trying to use FileIsExist to search the common directory of MQL, which is, C:\Users\Jim\AppData\Roaming\MetaQuotes\Terminal\9B101088254A9C260A9790D5079A7B11\MQL5\Files.

In the code below, it always defaults to the "else"  statement even if the file does not yet exist.


if the file does not exist yet, i want to create it and add a header, then do "something1"

If it already exists, then do not add a header,just do "something2".


appreciate if can point me to the right direction pls.

if (FileIsExist(myFile.csv)) {

 int fhandle=FileOpen("myFile.csv",FILE_READ|FILE_WRITE|FILE_CSV, ",");
               
                  if(fhandle==INVALID_HANDLE){
                     Alert(_Symbol+"Error opening file");
                     return; 
                  }
               FileSeek(fhandle, 0, SEEK_END);
do something1;
}

else { int fhandle=FileOpen("myFile.csv",FILE_READ|FILE_WRITE|FILE_CSV, ",");
               
                  if(fhandle==INVALID_HANDLE){
                     Alert(_Symbol+"Error opening file");
                     return; 
                  }
               FileSeek(fhandle, 0, SEEK_END);
do something2;
}
Documentation on MQL5: File Functions / FileIsExist
Documentation on MQL5: File Functions / FileIsExist
  • www.mql5.com
FileIsExist - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
if (FileIsExist(myFile.csv)) {

Do not post code that will not even compile.

 

Hi Guys.


Same issue,  hence not creating a new thread -  but also not the same issue...

Code below gets called from OnInit()

bool FileFunc()
{
   ResetLastError();
   
   if(!FileIsExist("fileName.csv"))
   {
      Print("File does not exist. Opening a new file.");
      
      int H_File = FileOpen("fileName.csv", FILE_CSV|FILE_UNICODE|FILE_WRITE, ",");

      if(H_File == INVALID_HANDLE)
      {
         Print("Error opening 'fileName.csv' - Error: ", _LastError);
         
         return false;
      }
      
      Print("File Opened.");      
      
      FileClose(H_File);
      
      Print("File Closed.");      
   }
   else
   {      
      if(!FileDelete("fileName.csv"))
      {
         Print("File Delete Failure - Error: ", _LastError);
         
         return false;
      }   
      else
         Print("File Deleted");                          
   }
   
   return true;   
}


Here's the funny thing. When attaching the EA to a chart, it works as expected.

2023.11.29 13:12:04.274 File does not exist. Opening a new file.

2023.11.29 13:12:04.275 File Opened.

2023.11.29 13:12:04.275 File Closed.

2023.11.29 13:14:19.907 File Deleted.

When running it in tester, it keeps on executing the 'true' section of FileIsExist over and over again.

Therefore, I have the same question: What am I missing?

Am I indeed missing something? 

Am I overlooking my own error?

Am I... Am I... Am I...


A pair of fresh eyes will be highly appreciated.


LMC

 
lmclark #:
FileFunc

How many times do you call FileFunc() ?


Looking at the output you shared, the deletion seems to be happening 2 mins later than the creation.

2023.11.29 13:12:04.275 File Closed.

2023.11.29 13:14:19.907 File Deleted.

Thus it is most likely is a another call, made after the file is created in the 1st call.

So in fact the "else" section of the code should indeed be executed because on the subsequent call, the file does exist.

You should set a break point and step through the code to ascertain whether that is what is happening

 
R4tna C #:

How many times do you call FileFunc() ?


Looking at the output you shared, the deletion seems to be happening 2 mins later than the creation.

2023.11.29 13:12:04.275 File Closed.

2023.11.29 13:14:19.907 File Deleted.

Thus it is most likely is a another call, made after the file is created in the 1st call.

So in fact the "else" section of the code should indeed be executed because on the subsequent call, the file does exist.

You should set a break point and step through the code to ascertain whether that is what is happening

Hi R4tna C #

FileFunc() is only called once in OnInit()

   if(!FileFunc())
      return INIT_FAILED;


The output which I've added to my original post was to demonstrate that the EA works as expected when attached to a chart. The 2min time-lapse is a delay between removing the EA from and re-attaching it to the chart.

Attach to chart when !FileIsExist == true

2023.11.29 13:12:04.274 File does not exist. Opening a new file.

2023.11.29 13:12:04.275 File Opened.

2023.11.29 13:12:04.275 File Closed.

Remove EA from chart, wait 2mins and then re-attach it. Now  !FileIsExist == false
2023.11.29 13:14:19.907 File Deleted.



The illogical behaviour occurs when running it in tester; as well as when debugging and stepping through the code.

I honestly don't get why !FileIsExist keeps on processing as true, irrespective whether the file exists or not {in tester}.

Thanks for getting back to me R4tna C #. Much appreciated.

I really hope that someone who has experienced the same or someone who can spot the mistake come across this thread.

LMC
 
lmclark #:
Hi R4tna C #

FileFunc() is only called once in OnInit()

   if(!FileFunc())
      return INIT_FAILED;


The output which I've added to my original post was to demonstrate that the EA works as expected when attached to a chart. The 2min time-lapse is a delay between removing the EA from and re-attaching it to the chart.

Attach to chart when !FileIsExist == true

2023.11.29 13:12:04.274 File does not exist. Opening a new file.

2023.11.29 13:12:04.275 File Opened.

2023.11.29 13:12:04.275 File Closed.

Remove EA from chart, wait 2mins and then re-attach it. Now  !FileIsExist == false
2023.11.29 13:14:19.907 File Deleted.



The illogical behaviour occurs when running it in tester; as well as when debugging and stepping through the code.

I honestly don't get why !FileIsExist keeps on processing as true, irrespective whether the file exists or not {in tester}.

Thanks for getting back to me R4tna C #. Much appreciated.

I really hope that someone who has experienced the same or someone who can spot the mistake come across this thread.

LMC

Sure - can't understand it either.

You may find changing your logic could overcome this - for example use FileOpen() first (which is common whether you create or open an existing file), and then use FileSize() to determine whether the file is empty or not.

From memory the FileSize() of a new file is 2 bytes, so you could use <= 2 as a determining factor (but please do check)

 
R4tna C #:

Sure - can't understand it either.

You may find changing your logic could overcome this - for example use FileOpen() first (which is common whether you create or open an existing file), and then use FileSize() to determine whether the file is empty or not.

From memory the FileSize() of a new file is 2 bytes, so you could use <= 2 as a determining factor (but please do check)

Thanks for the sneaky workaround R4tna C.

However - you know how it goes - temporary fixes becomes permanent features. Thus, I would prefer to rather get this sorted.

I am clearly doing something wrong 'cause I cannot believe that every MQL-Dev on this forum is experiencing the same problem as me.

Therefore, I went and trapped the error to see whether I might get a result which tells me straight-up that I'm doing something stupid!


The 'Tester' folder is empty: C:\Users\PC\AppData\Roaming\MetaQuotes\Tester\...\...\MQL5\Files\

The complete EA:


int OnInit()
{
   ResetLastError();
   
   if(FileIsExist("TestFile.CSV"))
   {
      Print("FileIsExists() == true");
      Print("Last Error: ", _LastError);
   }   
   else
   {
      Print("FileIsExists() == false");
      Print("Last Error: ", _LastError);
   }
   return INIT_SUCCEEDED; 
}



void OnDeinit(const int reason)
{
}

void OnTick()
{
}

Stepped trough the code, and as previously, I got the following result:

2023.12.01 13:26:55.887 2023.10.23 00:00:00   FileIsExists() == false
2023.12.01 13:26:56.358 2023.10.23 00:00:00   Last Error: 5019

Error 5019 = File does not exist. 

Great! It processed the correct logical block and produced the correct error.

Then, I went and manually created an empty "TestFile.CSV" in C:\Users\PC\AppData\Roaming\MetaQuotes\Tester\...\...\MQL5\Files\ and stepped through the code again, and yes, you've guessed it!

2023.12.01 14:33:33.966 2023.10.23 00:00:00   FileIsExists() == false
2023.12.01 14:33:34.425 2023.10.23 00:00:00   Last Error: 5019


I was dumbstruck! Went back to look in C:\Users\PC\AppData\Roaming\MetaQuotes\Tester\...\...\MQL5\Files\ and in the centre of the File Explorer window there was text saying "Working on it...". I refreshed the folder and there was no "TestFile.CSV" anymore.

This freaked me out!

So, again, I went and created another empty "TestFile.CSV" in C:\Users\PC\AppData\Roaming\MetaQuotes\Tester\...\...\MQL5\Files\ 

Opened the "TestFile.CSV" and closed it again. Closed the File Explorer window; open it again, and went to C:\Users\PC\AppData\Roaming\MetaQuotes\Tester\...\...\MQL5\Files\ 

OK! The file is still there. So, I went back and stepped trough the code - again: Debug stop set on ResetLastError();

Stepped into ResetLastError(); and with the cursor on if(FileIsExist("TestFile.CSV")) I went back to File Explorer, and guess what?!

Right in the middle of the File Exlorer window the same text "Working on it...". Refreshed the Files-folder again, and like magic, the file was gone. In the middle of the File Explorer window the words "This folder is empty."

No wonder FileIsExist() are processing false repeatedly 'cause there is no bloody file!


I am at wits end! If anyone else is as confused as I am, please raise your hand. If not, please tell me where I am missing the plot.


Anyone?


LMC

 
lmclark #:
Thanks for the sneaky workaround R4tna C.

However - you know how it goes - temporary fixes becomes permanent features. Thus, I would prefer to rather get this sorted.

I am clearly doing something wrong 'cause I cannot believe that every MQL-Dev on this forum is experiencing the same problem as me.

Therefore, I went and trapped the error to see whether I might get a result which tells me straight-up that I'm doing something stupid!


The 'Tester' folder is empty: C:\Users\PC\AppData\Roaming\MetaQuotes\Tester\...\...\MQL5\Files\

The complete EA:



Stepped trough the code, and as previously, I got the following result:

2023.12.01 13:26:55.887 2023.10.23 00:00:00   FileIsExists() == false
2023.12.01 13:26:56.358 2023.10.23 00:00:00   Last Error: 5019

Error 5019 = File does not exist. 

Great! It processed the correct logical block and produced the correct error.

Then, I went and manually created an empty "TestFile.CSV" in C:\Users\PC\AppData\Roaming\MetaQuotes\Tester\...\...\MQL5\Files\ and stepped through the code again, and yes, you've guessed it!

2023.12.01 14:33:33.966 2023.10.23 00:00:00   FileIsExists() == false
2023.12.01 14:33:34.425 2023.10.23 00:00:00   Last Error: 5019


I was dumbstruck! Went back to look in C:\Users\PC\AppData\Roaming\MetaQuotes\Tester\...\...\MQL5\Files\ and in the centre of the File Explorer window there was text saying "Working on it...". I refreshed the folder and there was no "TestFile.CSV" anymore.

This freaked me out!

So, again, I went and created another empty "TestFile.CSV" in C:\Users\PC\AppData\Roaming\MetaQuotes\Tester\...\...\MQL5\Files\ 

Opened the "TestFile.CSV" and closed it again. Closed the File Explorer window; open it again, and went to C:\Users\PC\AppData\Roaming\MetaQuotes\Tester\...\...\MQL5\Files\ 

OK! The file is still there. So, I went back and stepped trough the code - again: Debug stop set on ResetLastError();

Stepped into ResetLastError(); and with the cursor on if(FileIsExist("TestFile.CSV")) I went back to File Explorer, and guess what?!

Right in the middle of the File Exlorer window the same text "Working on it...". Refreshed the Files-folder again, and like magic, the file was gone. In the middle of the File Explorer window the words "This folder is empty."

No wonder FileIsExist() are processing false repeatedly 'cause there is no bloody file!


I am at wits end! If anyone else is as confused as I am, please raise your hand. If not, please tell me where I am missing the plot.


Anyone?


LMC

Sneaky workarounds are my favourite  :)

Anyway - I have noticed there can be a delay in Windows updating, even outside MQL - hitting F5 is necessary.

I do not recall hitting this exact problem in MQL5 but some time back I included the FileFlush() command in my code to force a write to disk, to minimize the chance. Why not give it a try?

https://www.mql5.com/en/docs/files/fileflush

Documentation on MQL5: File Functions / FileFlush
Documentation on MQL5: File Functions / FileFlush
  • www.mql5.com
FileFlush - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
R4tna C #:

Sneaky workarounds are my favourite  :)

Anyway - I have noticed there can be a delay in Windows updating, even outside MQL - hitting F5 is necessary.

I do not recall hitting this exact problem in MQL5 but some time back I included the FileFlush() command in my code to force a write to disk, to minimize the chance. Why not give it a try?

https://www.mql5.com/en/docs/files/fileflush

I am aware of what you are referring to w.r.t. refresh, but as I mentioned in my previous post, I created the file (outside of the IDE); opened and closed it after creation; closed file explorer and re-opened it; went back to the relevant folder to check that the file is still there.

Which it was. So, the file was committed to disk.

FileFlush() would only be relevant of the file was created by FileOpen(). Never the less, I changed the code:

int OnInit()
{
   ResetLastError();
   
   if(FileIsExist("TestFile.CSV"))
   {
      if(!FileDelete("TestFile.CSV"))
         Print("File Delete Error - ", _LastError);
   }   
   else
   {
      int H_File = FileOpen("TestFile.CSV", FILE_CSV|FILE_WRITE|FILE_UNICODE, ",");
      
      if(H_File == INVALID_HANDLE)
      {
         Print("File Open Error - ", _LastError);
         
         return false;
      }
      
      FileFlush(H_File);
      
      FileClose(H_File);
   }
   return INIT_SUCCEEDED; 
}



void OnDeinit(const int reason)
{
}

void OnTick()
{
}


First execution, without the file existing in ...\Files, FileIsExist() acts as expected and process the false block.


Check - the file was created successfully.


Second execution with the file existing in ...\Files, FileIsExist() process the false block again.


<sigh>



LMC

 
lmclark #:

I am aware of what you are referring to w.r.t. refresh, but as I mentioned in my previous post, I created the file (outside of the IDE); opened and closed it after creation; closed file explorer and re-opened it; went back to the relevant folder to check that the file is still there.

Which it was. So, the file was committed to disk.

FileFlush() would only be relevant of the file was created by FileOpen(). Never the less, I changed the code:


First execution, without the file existing in ...\Files, FileIsExist() acts as expected and process the false block.


Check - the file was created successfully.


Second execution with the file existing in ...\Files, FileIsExist() process the false block again.


<sigh>



LMC

Yes - some functions may not be 100% reliable (in another thread there was a discussion of conversion functions which seem to be lacking, prompting many to write their own).

I suspect you need to consider FileIsExist() has such quirks and thus the workaround of reading the size could be a more reliable approach

 
lmclark #:

Hi Guys.


Same issue,  hence not creating a new thread -  but also not the same issue...

Code below gets called from OnInit()


Here's the funny thing. When attaching the EA to a chart, it works as expected.

2023.11.29 13:12:04.274 File does not exist. Opening a new file.

2023.11.29 13:12:04.275 File Opened.

2023.11.29 13:12:04.275 File Closed.

2023.11.29 13:14:19.907 File Deleted.

When running it in tester, it keeps on executing the 'true' section of FileIsExist over and over again.

Therefore, I have the same question: What am I missing?

Am I indeed missing something? 

Am I overlooking my own error?

Am I... Am I... Am I...


A pair of fresh eyes will be highly appreciated.


LMC

This is normal behaviour using MQL5/Files with the Strategy Tester. The files there are deleted on each run, so your file never exists. I suppose it's written somewhere in the chaotic documentation.

If you want your file(s) to remain between different tester runs, then use the FILE_COMMON flag to use the common folder.

Reason: