Download MetaTrader 5

Unable to run batch file

To add comments, please log in or register
partha
16
partha  

Hi, I am trying to run a batch file from EA, but it's not working. The full text is inserted below. Can anybody help? Appreciate a direct ans i/o link to another forum

 

#include <stdlib.mqh>

#include <WinUser32.mqh>

#import "shell32.dll"

int ShellExecuteW(int hwnd,const string Operation,const string File,const string Parameters,const string Directory,int ShowCmd);

#import


void OnInit()

{

}

void Start()

{

   int r = ShellExecuteW(0,"Open","C:\Users\Partha\AppData\Roaming\MetaQuotes\Terminal\D42A6903521AA0A52E40B0910C7C2934\MQL4\Files\c.bat",NULL,NULL,5);

   Print("Shell",r);

}

 c.bat:

@echo off

copy C:\Users\Partha\AppData\Roaming\MetaQuotes\Terminal\D42A6903521AA0A52E40B0910C7C2934\MQL4\Files\TradeCopy.csv C:\Users\Partha\AppData\Roaming\MetaQuotes\Terminal\D22174118A10226954F7121FC08B77A4\MQL4\Files /y > copy.txt

whroeder1
16076
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Your shell call specifies c.bat, but that is not an executable file, so it fails. Try:
    // int r = ShellExecuteW(0,"Open","C:\Users\Partha\AppData\Roaming\MetaQuotes\Terminal\D42A6903521AA0A52E40B0910C7C2934\MQL4\Files\c.bat",NULL,NULL,5);
       int r = ShellExecuteW(0,"Open","cmd.exe","/C C:\\Users\\Partha\\AppData\\Roaming\\MetaQuotes\\Terminal\\D42A6903521AA0A52E40B0910C7C2934\\MQL4\\Files\\c.bat",NULL,5);
Ex Ovo Omnia
3354
Ex Ovo Omnia  
Partha:


   int r = ShellExecuteW(0,"Open","C:\Users\Partha\AppData\Roaming\MetaQuotes\Terminal\D42A6903521AA0A52E40B0910C7C2934\MQL4\Files\c.bat",NULL,NULL,5);

 

Escape all the back slashes.

whroeder1
16076
whroeder1  
Ovo: Escape all the back slashes.
That also
partha
16
partha  

Thanx WHRoeder and Ovo for the valuable inputs. After quite a bit of trial and errors, It's working now. For benefit of other users, here's the complete code:

#include <WinUser32.mqh>
#import "shell32.dll"
int ShellExecuteW(int hwnd,const string Operation,const string File,const string Parameters,const string Directory,int ShowCmd);
#import

void OnInit()
{
   string terminalDataPath=TerminalInfoString(TERMINAL_DATA_PATH);
   int r = ShellExecuteW(0,"Open","c.bat",NULL,terminalDataPath,5);
   Print("Shell ",r);
}

 c.bat (located in "C:\Users\Partha\AppData\Roaming\MetaQuotes\Terminal\D42A6903521AA0A52E40B0910C7C2934" folder): 

@echo off
copy C:\Users\Partha\AppData\Roaming\MetaQuotes\Terminal\D42A6903521AA0A52E40B0910C7C2934\MQL4\Files\TradeCopy.csv C:\Users\Partha\AppData\Roaming\MetaQuotes\Terminal\D22174118A10226954F7121FC08B77A4\MQL4\Files /y > copy.txt

Also, need to make sure that DLL imports are enabled in MT4.

One more thing. Alternatively cmd.exe may also be used. So the statement would be like: 

int r = ShellExecuteW(0,"Open","cmd.exe","/C c.bat",terminalDataPath,5)

huatboyz
175
huatboyz  
Thanks Partha for sharing.. This code works great. God bless all..
To add comments, please log in or register