Failure calling working cmd.exe command from within a script

 

Hi,


as noted in this recent thread of mine, I'm trying to create a script that will copy my developed .ex5 files to other terminal's folders. After a failure attempt of doing that with MQL5's own functions, I tried appealing to classic cmd.exe commands. 

With that in mind, I successfully created a .bat file that does what I want with the following code:

copy "G:\teste\teste.txt" "G:\teste2"


After, following the example lay down by this thread, I created a MQL5 script with the following code:

#property script_show_inputs

#import "shell32.dll" 
int ShellExecuteW(int hwnd,string operation,string file,string parameters,string directory,int showCmd); 
int ShellExecuteA(int hwnd,string Operation,string File,string Parameters,string Directory,int ShowCmd);
#import

void OnStart()
{
Print("MW UW: Start");

ShellExecuteA(0,"open","cmd.exe","copy \"G:\\teste\\teste.txt\" \"G:\\teste2\"","",0);

Print("MW UW: End");
}


And guess what, it didn't work :( What am I doing wrong?

Writting script that copies files from one terminal data folder to another
Writting script that copies files from one terminal data folder to another
  • 2021.08.06
  • www.mql5.com
Hi, I have 2 MT5 Terminals in my pc, one for development with a demo account and another for work with real account...
 
Martin Bittencourt: What am I doing wrong?
ShellExecuteA(0,"open","cmd.exe","copy \"G:\\teste\\teste.txt\" \"G:\\teste2\"","",0);
  1. Since build 600 and Higher (2014.02.03) all strings are Unicode. Wrong call.
              Syntax for calling AutoIt compiled Script (.exe) using ShellExecuteW in MT4 EA (Quest) - MQL4 programming forum (2015.04.25)
              run a *.bat from EA (Route206) - MQL4 programming forum (2015.10.30)
              Execute an exe-file from MQL (Steffen Siegert) - MQL4 programming forum (2016.07.30)

  2. Open a cmd window and type in "cmd.exe echo xx" and you will know why the call will not work.

 
William Roeder:
  1. Since build 600 and Higher (2014.02.03) all strings are Unicode. Wrong call.
              Syntax for calling AutoIt compiled Script (.exe) using ShellExecuteW in MT4 EA (Quest) - MQL4 programming forum (2015.04.25)
              run a *.bat from EA (Route206) - MQL4 programming forum (2015.10.30)
              Execute an exe-file from MQL (Steffen Siegert) - MQL4 programming forum (2016.07.30)

  2. Open a cmd window and type in "cmd.exe echo xx" and you will know why the call will not work.

Hi! Thanks for the reply.

I changed the call of ShellExecuteA to ShellExecuteW, but it didn't work either. And I "open a cmd window and type in "cmd.exe echo xx"" and I only got was a message about the current windows version, nothing I could make use of to solve my problem:

cmd

What am I missing? :|

 
Martin Bittencourt:

Hi! Thanks for the reply.

I changed the call of ShellExecuteA to ShellExecuteW, but it didn't work either. And I "open a cmd window and type in "cmd.exe echo xx"" and I only got was a message about the current windows version, nothing I could make use of to solve my problem:


What am I missing? :|

Are you kidding ? The "xx" is to be replaced by your command of course.
 
And anyway you are overcomplicating things. A simple junction would suffice to get what you want.
 
Alain Verleyen:
Are you kidding ? The "xx" is to be replaced by your command of course.

Gosh how could I know that? xD

Anyway, it didn't quite improved my situation ^^ 

attempts

P.S: Let's remember, the copy command works in cmd A.w.a. when called from withing a .bat file. It's only when I call it with ShellExecuteW from within the MQL script that nothing happens.

 
The xx did not need to be replaced. "cmd command" only starts a new cmd. Type "help cmd" and read about the /C flag.
 
Alain Verleyen:
And anyway you are overcomplicating things. A simple junction would suffice to get what you want.

Hmm and what is a junction? xD

 
William Roeder:
The xx did not need to be replaced. "cmd command" only starts a new cmd. Type "help cmd" and read about the /C flag.

Hmmm so it was the /c that was missing :3 I saw in one of the examples in one of the link you provided, but though it was something specific of that case and didn't add it. Thanks! :)

(My gosh, all this trouble because of a missing /c \o/)

 
Martin Bittencourt:

Gosh how could I know that? xD

Anyway, it didn't quite improved my situation ^^ 


P.S: Let's remember, the copy command works in cmd A.w.a. when called from withing a .bat file. It's only when I call it with ShellExecuteW from within the MQL script that nothing happens.

Ok sorry, I misunderstood William's remark.

Martin Bittencourt:

Hmm and what is a junction? xD

https://docs.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions
Hard Links and Junctions - Win32 apps
Hard Links and Junctions - Win32 apps
  • 2018.05.31
  • mikben
  • docs.microsoft.com
Describes hard links and junctions.
Reason: