Unable to run batch file

Back to topics list To post a new topic, please log in or register
avatar
6
Partha 2015.04.10 08: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

 

 

avatar
13006
WHRoeder 2015.04.10 14: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);

avatar
1110
Ovo 2015.04.10 17: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.

avatar
13006
WHRoeder 2015.04.10 19:57 #
 
Ovo: Escape all the back slashes.
That also

avatar
6
Partha 2015.04.14 07: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)

avatar
3
huatboyz 2015.06.15 08:23 #
 
Thanks Partha for sharing.. This code works great. God bless all..
Back to topics list  

To add comments, please log in or register