How to resolve a history folder

 

I have a question.

Is there any elegant way of how to resolve a current history folder name, in which the .hst files are accessed if no internet connection is open?

Since there is another bug with FileOpenHistory() (I mentioned earlier in the forum), I have to use the Windows API for updating offline charts.

Under normal condition, i.e. when connected to a trading server, there is a function AccountServer() that returns the name of the folder (at least I did not encounter any exception).

On the other hand, when starting without internet connection (with auto login), the function AccountServer() returns an empty string, though the hst data are still correctly accessed by the terminal itself and the FileOpenHistory() function.

I use a workaround, traversing content of the history folder, but I consider it tricky and like to find some other way. Any suggestion appreciated.

 
Ovo:

I have a question.

Is there any elegant way of how to resolve a current history folder name, in which the .hst files are accessed if no internet connection is open?

Since there is another bug with FileOpenHistory() (I mentioned earlier in the forum), I have to use the Windows API for updating offline charts.

This ? https://www.mql5.com/en/forum/142700/page4#760662

Are you adding the dots for your offline chart ? why not add something other than the dots ?

 

Even the regular history files have double dots in their names with that particular broker. If I alter the symbol name I cannot trade from the chart.

 
Ovo:

Even the regular history files have double dots in their names with that particular broker. If I alter the symbol name I cannot trade from the chart.

Ah I see. You can still create your offline charts with a name of your choice, for example: EURUSDoff . . . then you just have to remove the "off" and add in the dots when you place your trade . . .
 
RaptorUK:
Ah I see. You can still create your offline charts with a name of your choice, for example: EURUSDoff . . . then you just have to remove the "off" and add in the dots when you place your trade . . .

Lol, in such case it is more acceptable to start the indicator with a warning if the internet connection is off :) Actually I am looking for a safe and convenient solution, without need of advices like "never do that" or "I strongly recommend not to". Just plug and play script. But I appreciate your inputs anyway.
 

A solution for reference. First of all, we can use FileOpenHistory()  to make a file in the current history folder. Then find that file with FindFirstFileW and FindNextFileW, so that you can get the actual current history folder. 

#import "kernel32.dll"
   int      FindFirstFileW(string Path,ushort &Answer[]);
   bool     FindNextFileW(int handle,ushort &Answer[]);
   bool     FindClose(int handle);
   int      DeleteFileW(string file);
#import

#define FILE_ATTRIBUTE_DIRECTORY 16

void start()
  {
   string name=GetHistoryDir();
   printf(name);
  }


string GetHistoryDir()
  {
   string markFile="tofindhst";
   int h=FileOpenHistory(markFile,FILE_BIN|FILE_WRITE|FILE_READ|FILE_SHARE_WRITE|FILE_SHARE_READ);
   FileClose(h);
  
   ushort Buffer[300];
   ArrayInitialize(Buffer,0);
   
   string HistoryPath=TerminalPath()+"\\history";
   int handle=FindFirstFileW(HistoryPath+"\\*.*",Buffer);
   bool isFound=false;
   string dirName="";
   if(handle>0)
     {
      do{
         int filesize=(int)Buffer[16]+(int)Buffer[17]*USHORT_MAX;
         if(filesize==0)
           {
            dirName=ShortArrayToString(Buffer,22,152);
            if(dirName!="." && dirName!="..")
              {
               isFound=FindFilesInSubDir_(HistoryPath+"\\"+dirName,markFile);
               if(isFound==true) break;
              }
           }      
         ArrayInitialize(Buffer,0);
        } while(FindNextFileW(handle,Buffer));
        
      FindClose(handle);
     }
   return dirName;
  }

bool FindFilesInSubDir_(string subDirPath, string markFilename)
  {
   bool is_Found=false;
   ushort Result[300];
   ArrayInitialize(Result,0);
   int hand_sub=FindFirstFileW(subDirPath+"\\*hst",Result);
   if(hand_sub>0)
     {
      do{
         string fileName=ShortArrayToString(Result,22,152);
         if(fileName==markFilename) is_Found=true;
         ArrayInitialize(Result,0);
        } while(FindNextFileW(hand_sub,Result));
     }
   FindClose(hand_sub);
   if(is_Found==true) 
     {
      DeleteFileW(subDirPath+"\\"+markFilename);
     }
   return is_Found;
  }