MQL4 User directory / %USERNAME% variable string

To add comments, please log in or register
Chris
19
Chris  

Hello,

I was wondering if there was a better way to retrieve the current user directory?

In Batch file (*.bat) its as simple as placing %USERNAME% in the desired directory path

In MQL4 I was unable to find a similar variable string


My current solution is:

int OnInit()
{
  string original_data_path=TerminalInfoString(TERMINAL_COMMONDATA_PATH);
  string common_data_path=TerminalInfoString(TERMINAL_COMMONDATA_PATH);
  // The above returns "C:\Users\myUSERNAME\AppData\Roaming\MetaQuotes\Terminal\Common"

  StringReplace(common_data_path,"\\AppData\\Roaming\\MetaQuotes\\Terminal\\Common","");
  // The above returns "C:\Users\myUSERNAME"

  StringReplace(common_data_path,"C:\\Users\\","");
  // The above returns "myUSERNAME"

  Comment(original_data_path,"\n",common_data_path);
}

  // https://docs.mql4.com/constants/environment_state/terminalstatus
  // https://www.mql5.com/en/forum/153721#comment_3791042

 

TERMINAL_COMMONDATA_PATH is the only consistent variable I found that contains the USERNAME file directory

StringReplace is used to remove the excess directories (before and after) leaving myUSERNAME string.

The reason I am trying to find the username directory variable is because I want all my EA's to send screenshots to Google Drive/ Dropbox


Kind regards,
Chris

Client Terminal Properties - Environment State - Constants, Enumerations and Structures - MQL4 Reference
Client Terminal Properties - Environment State - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
Information about the client terminal can be obtained by two functions: TerminalInfoInteger() and TerminalInfoString(). For parameters, these functions accept values from ENUM_TERMINAL_INFO_INTEGER and ENUM_TERMINAL_INFO_STRING respectively. Knowing the parameter value, you can set the size of graphical objects so that they look the...
William Roeder
20251
William Roeder  
Chris: I was wondering if there was a better way to retrieve the current user directory?
Why do you care? You can only read/write to the Data Folder and Common directories.
Chris
19
Chris  
William Roeder:
Why do you care? You can only read/write to the Data Folder and Common directories.
Hi William,

Chris:

The reason I am trying to find the username directory variable is because I want all my EA's to send screenshots to Google Drive/ Dropbox


I have been successfully writing screenshots to the following directory:

C:\Users\ ~myUSERNAME~ \Google Drive\Screenshots

I have a sever and two laptops that all have different username directories.
When I load my EA onto a different device I have to manually configure it to have a different ~myUSERNAME~
(otherwise the screenshot will not save)

For example...

C:\Users\LAPTOP\Google Drive\Screenshots
C:\Users\SERVER\Google Drive\Screenshots
C:\Users\ADMIN\Google Drive\Screenshots
To add comments, please log in or register