Unable to write (or move) file with .js extension

 
//+------------------------------------------------------------------+
//|                                               JavascriptTest.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    const int handle = FileOpen("JavascriptFile.js", FILE_WRITE | FILE_TXT );

    if (handle != INVALID_HANDLE)
    {
      FileWrite(handle, "var data = 123;");
      FileClose(handle);
    } else {
      Print ("Write Error ", GetLastError());
    }
    // Error 5002 (== Invalid file name)

    const int handle2 = FileOpen("JavascriptFile.jsX", FILE_WRITE | FILE_TXT );

    if (handle2 != INVALID_HANDLE)
    {
      FileWrite(handle2, "var data = 123;");
      FileClose(handle2);
    } else {
      Print ("Write Error ", GetLastError());
    }
    // No error
    
    if (!FileMove("JavascriptFile.jsX",0,"JavascriptFile.js",FILE_REWRITE))
      Print ("Move Error ", GetLastError());
    // Error 5002 (== Invalid file name)
  }
//+------------------------------------------------------------------+

Is this an (undocumented) MQL5 limitation or is my windows preventing writing files with .js extension?

 
Same result MT4 (build 1260)
 

Thanks for checking William. Still wondering about whether this is an MQL or Windows issue.

Edit to add, there is nothing in the Windows Event Viewer to be seen.

 
You can just drop the extension and call the script in the script tag without it. (also i think it's FILE_BIN)
 
Marco vd Heijden:
You can just drop the extension and call the script in the script tag without it. (also i think it's FILE_BIN)

It is not an option to ditch the extension. No browser would run a non .js as Javascript include.

A JavaScript file is a text file, not a binary.  

 

I use it like that all the time.

And i write it from the EA to the folder as FILE_BIN from a char array[];

FYI:

//--- open new file
   int filewritehandle = FileOpen(filename,FILE_READ|FILE_WRITE|FILE_BIN);

//--- inject head css blah blah etc..

//--- inject javascript engine script
   script = "<script type = \"text/JavaScript\">\n";
   FileSeek(filewritehandle,0,SEEK_END);
   FileWriteString(filewritehandle, script, StringLen(script));

   FileSeek(filewritehandle,0,SEEK_END);
   FileWriteArray(filewritehandle,enginejs,0,ArraySize(enginejs));

   script = "</script>\n";
   FileSeek(filewritehandle,0,SEEK_END);
   FileWriteString(filewritehandle, script, StringLen(script));

//--- 

//+------------------------------------------------------------------+
char enginejs[] = {47,42,33,10,32,42,32,67,104,97,114,116,46,106,115,32,118,50,46,57,46,51,10,32,42,32,104,116,116,112,
                 115,58,47,47,119,119,119,46,99,104,97,114,116,106,115,46,111,114,103,10,32,42,32,40,99,41,32,50,48,49,
                 57,32,67,104,97,114,116,46,106,115,32,67,111,110,116,114,105,98,117,116,111,114,115,10,32,42,32,82,101,108,
                 101,97,115,101,100,32,117,110,100,101,114,32,116,104,101,32,77,73,84,32,76,105,99,101,110,115,101,10,32,42,
                 47,10,40,102,117,110,99,116,105,111,110,32,......

You can probably also just hard code it no i mean just put in a link to another source that serves the file in the html to save you the hassle. 

Documentation on MQL5: Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
Documentation on MQL5: Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
  • www.mql5.com
CSV file (all its elements are converted to strings of the appropriate type, Unicode or ANSI, and separated by separator). Flag is used in FileOpen(). Shared access for reading from several programs. Flag is used in FileOpen(), but it does not replace the necessity to indicate FILE_WRITE and/or the FILE_READ flag when opening a file. Shared...
 
Enrique Dangeroux:

Thanks for checking William. Still wondering about whether this is an MQL or Windows issue.

Edit to add, there is nothing in the Windows Event Viewer to be seen.

This is clearly an mql feature.

.ex4,

.reg

Ошибки, баги, вопросы
Ошибки, баги, вопросы
  • 2012.01.06
  • www.mql5.com
Общее обсуждение: Ошибки, баги, вопросы
 
Marco vd Heijden:

I use it like that all the time.

And i write it from the EA to the folder as FILE_BIN from a char array[];

FYI:

You can probably also just hard code it no i mean just put in a link to another source that serves the file in the html to save you the hassle. 

It is no problem to write a .html file with or without all the malware javascript you like embeded in it. It is not possible to write a .js file. Ah well... Buy high, sell low..


For anyone looking for solution:

//+------------------------------------------------------------------+
//|                                               JavascriptTest.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#import "kernel32.dll"
   int MoveFileW(string ExistingFilename, string NewFilename);
   int CopyFileW(string strExistingFile, string strCopyOfFile, int OverwriteIfCopyAlreadyExists);
   int GetLastError();
#import

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    const int handle2 = ::FileOpen("JavascriptFile.jsX", FILE_WRITE | FILE_TXT );

    if (handle2 != INVALID_HANDLE)
    {
      FileWrite(handle2, "var data = 123; ");
      FileClose(handle2);
    } else {
      Print ("Write Error ", GetLastError());
    }
    
    string FileFrom = "JavascriptFile.jsX";
    
    if (FileIsExist(FileFrom))
      Print (FileFrom, " Found!");
    else
      Print (FileFrom, " not Found");

    string PathFileFrom = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\" + FileFrom;
    string PathFileTo   = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\" + "JavascriptFile.js";

    Print (PathFileFrom);
    
    //MoveFileW(PathFileFrom, PathFileTo);
    //Print("MoveFileW error ",kernel32::GetLastError());

    CopyFileW(PathFileFrom, PathFileTo,0);
    Print("CopyFileW error ",kernel32::GetLastError());    
  }
//+------------------------------------------------------------------+
 

I hate to use the kernel DLL.

That is opening up a real security hole.

Reason: