MT4 Script File path issues

 

I have writing a script and it work fine if I have the File path set as...   string mySpreadsheet= "Account.csv"    it will save the file in the files folder in MQL$>Files like I would expect Fig1 but if I change the file destination folder, to say       string mySpreadsheet= "G:\\My Drive\\Account.csv"    the script runs ok and I get the end message that the file has been saved but when I look in the folder it hasn't appeared, I have tried various file locations but to no avail.

void OnStart()
{
    // set file name and path
    string mySpreadsheet="G:\\My Drive\\AccountHistory.csv";
    
// open the file for writing in append mode, as CSV format, ANSI mode
int mySpreadsheetHandle = FileOpen(mySpreadsheet, FILE_WRITE|FILE_CSV|FILE_ANSI|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_REWRITE );



    // write the header row
    FileWrite(mySpreadsheetHandle, "Ticket", "Symbol", "Type", "Lots", "Open Price", "Stop Loss","Take Profit", "Close Price",  "Profit", "Commission", "Open Time", "Close Time");

    // get the number of history orders
    int ordersTotal = OrdersHistoryTotal();

    // loop through the history orders
    for (int i = ordersTotal - 1; i >= 0; i--)
    {
        // select the order by index
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
            // write the order data to the file
            FileWrite(mySpreadsheetHandle,
                OrderTicket(),
                OrderSymbol(),
                (OrderType() == OP_BUY ? "Buy" : "Sell"),
                OrderLots(),              
                OrderOpenPrice(),
                OrderStopLoss(),
                OrderTakeProfit(),
                OrderClosePrice(),
                OrderProfit(),
                OrderCommission(),
                TimeToString(OrderOpenTime()),
                TimeToString(OrderCloseTime())              
                                
                
            );
        }
    }

    // close the file
    FileClose(mySpreadsheetHandle);

    // show a message when done
    Alert("Account history data written to file: ", mySpreadsheet);
}



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

Please EDIT your post and use the CODE button when you insert code.

Code button in editor

 
  1. Use the code button

  2. Terry McCall: change the file destination folder, to say       string mySpreadsheet= "G:\\My Drive\\Account.csv"   

    File Write Problem (Error: 5002) - Expert Advisors and Automated Trading - MQL5 programming forum #1-2 (2020)
    and FolderDelete using TERMINAL_DATA_PATH - General - MQL5 programming forum (2017)

 
Terry McCall:I have writing a script and it work fine if I have the File path set as...   string mySpreadsheet= "Account.csv"    it will save the file in the files folder in MQL$>Files like I would expect Fig1 but if I change the file destination folder, to say       string mySpreadsheet= "G:\\My Drive\\Account.csv"    the script runs ok and I get the end message that the file has been saved but when I look in the folder it hasn't appeared, I have tried various file locations but to no avail.

In MQL, file access is "sandboxed". You can't directly access files on any path (.e.g. "G:\\My Drive\\AccountHistory.csv" will fail).

You can only access files in the "sandbox". Please read the documentation ... https://docs.mql4.com/files/fileopen

Note

For security reasons, work with files is strictly controlled in the MQL4 language. Files with which file operations are conducted using MQL4 means, cannot be outside the file sandbox.

The file is opened in the folder of the client terminal in the subfolder MQL4\files (or Tester\Files in case of testing). If FILE_COMMON is specified among flags, the file is opened in a shared folder for all MetaTrader 4 client terminals.

Please also read the links that William has provide in the previous post.

 
Thank you I read Williams comments and am currently working on a different solution.