Download MetaTrader 5
To add comments, please log in or register
No time to learn MQL5? Order your application from a developer!
partha
16
partha 2015.04.10 06:32 

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
13597
whroeder1 2015.04.10 12:24  
  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);
Ovo Cz
2955
Ovo Cz 2015.04.10 15:20  
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
13597
whroeder1 2015.04.10 17:57  
Ovo: Escape all the back slashes.
That also
partha
16
partha 2015.04.14 05:22  

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
101
huatboyz 2015.06.15 06:23  
Thanks Partha for sharing.. This code works great. God bless all..
/
To add comments, please log in or register