I'm using ShellExecuteW from shell32.dll:
int value= ShellExecuteW(0, "open", "C:\test.bat", strParameters, "", 1);
The batch file runs a java app which seems to open but returns an error and quickly the console window closes.
I want to capture the error from the console. I've tried adding the following re-directions at the end of my command in the batch file:
> output.txt
2> output.txt
> output.txt 2>&1
1> output.txt 2>&1
| output.txt
I would expect these common commands to work but none of them result in anything being written in output.txt. What could I be doing wrong here?
I'm using Metatrader5 (MQL5 language) to call shellexecuteW from.
Thankyou for your replies.
First you should put the bat file in Terminal path in "File" folder because of mql5 security policy, Secondly put these codes in your script to use "ShellExecuteW" function correctly.
#import "shell32.dll"
int ShellExecuteW(int hwnd,string operation,string file,string parameters,string directory,int showCmd);
#import
int result;
string strParameters = "";
string filename="test.bat";
string Batfile_path=TerminalInfoString(TERMINAL_COMMONDATA_PATH)+"\\Files\\"+filename;
Print("Batfile_path:",Batfile_path);
result = ShellExecuteW(0, "open", Batfile_path, strParameters, "", 0);
if(result <= 32)Print("Shell Execute for running bat file Failed: ", result);
Related
I include an external .ps1 into antother .ps1:
foo.ps1:
.("C:\test\bar.ps1");
$obj = [bar]::new();
$obj.out();
bar.ps1:
class bar{
$output;
bar(){
$this.output = 1;
}
[void] out(){
write-host $this.output;
}
}
The first time I execute foo.ps1 in the Windows PowerShell ISE the output is "1", as expected.
Then I go to bar.ps1 and change $this.output = 1; to $this.output = 2;. After executing foo.ps1 again the output is still "1". When I change something in foo.ps1, like simply appending a new line, and execute it once again, the output becomes "2". Changing back, like removing the new line, will make an output of "1" again.
For me it looks like an caching issue. Is it possible to clear or disable the caching?
Thanks in advance!
The powershell ise sometimes prints out my source code, if I have:
function f
{
$a=2
}
$a
It prints:
C:\Users\vics> function f
{
$a=2
}
$a
Why so weired?
If you are not saving your files, the code is written down into the console window. If your file is saved, it is just executed. You will then see the execution path like
C:\Users\vics> C:\Users\vics\Documents\test.ps1
...
In a mongo shell window, I'd like to periodically run a script that will display various stats on the database activity, before displaying the stats, I'd like to clear the screen. There is a "cls" command in the mongo shell, but I am not able to execute it from within the javascript.
function stats () {
while(1) {
cls;
print("display stats");
sleep(5000);
}}
The line with the "cls" is not recognized.
Thank you for any suggestions,
Gary
At the first glance it seemed that you won't be able to do it. According to the docs here: "You cannot use any shell helper (e.g. use , show dbs, etc.) inside the JavaScript file because they are not valid JavaScript.".
One option was to fill the screen with empty lines:
function clearIt () { for(var i = 0; i < 100; i++) { print() } }
clearIt()
However, thanks to #NeilLunn pointing it out there seems to be a solution:
function clearIt () { run('clear') }
clearIt()
This would execute system command which will clear your terminal screen. I don't know how reliable it is (see man clear -> depends if it can figure out how to clear screen) and this works only on POSIX systems. On Windows you would have to replace clear with cls:
function clearIt () { run('cls') }
Additional:
I looked up the source code of mongo shell (src/mongo/shell/linenoise.cpp). Here is how it clears the screen:
void linenoiseClearScreen( void ) {
#ifdef _WIN32
COORD coord = {0, 0};
CONSOLE_SCREEN_BUFFER_INFO inf;
HANDLE screenHandle = GetStdHandle( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo( screenHandle, &inf );
SetConsoleCursorPosition( screenHandle, coord );
DWORD count;
FillConsoleOutputCharacterA( screenHandle, ' ', inf.dwSize.X * inf.dwSize.Y, coord, &count );
#else
if ( write( 1, "\x1b[H\x1b[2J", 7 ) <= 0 ) return;
#endif
}
In case you feel like trying to implement your own screen cleaning function by filling screen with chars.
> help admin
ls([path]) list files
pwd() returns current directory
listFiles([path]) returns file list
hostname() returns name of this host
cat(fname) returns contents of text file as a string
removeFile(f) delete a file or directory
load(jsfilename) load and execute a .js file
run(program[, args...]) spawn a program and wait for its completion
runProgram(program[, args...]) same as run(), above
sleep(m) sleep m milliseconds
getMemInfo() diagnostic
This shows the run and runProgram commands along with some other helpers. The program argument is a string.
I want to execute a command and parse the output from the shell. I am using JScript inside TestComplete. I already found out that I can run commands using WScript.shell. But I do not know how to parse the output in my JScript. Any hints?
var shell = new ActiveXObject("WScript.shell");
if (shell)
{
shell.run("myCommandIWantToParseOutputfrom.sh");
}
Take a look at the Exec method instead of Run.
var wsh = new ActiveXObject("WScript.Shell");
var cmd = wsh.Exec("cmd /c dir C:\ /on");
while (cmd.Status === 0) {
WScript.Sleep(100);
}
var output = cmd.StdOut.ReadAll();
WScript.Echo(output);
I want to type directly into the Powershell prompt too, not pipe in a text file.
Foo`r doesn't work for me. For example:
echo "RJ`r`n" | .\nc.exe -u 192.168.1.247 2639
but what I'd really like to do is just
.\nc.exe -u 192.168.1.247 2639
then start typing into the prompt.
Try:
"Foo`ndoes work for me"
If you need a full CRLF sequence then:
"Foo`r`ndoes work for me"
Note that the escapes chars only work in double-quoted strings - not single quoted strings.
Update: Redirect in < is not supported in PowerShell at this time so you can only get stdin to the exe from the interactive prompt using the pipeline. Is it possible nc.exe is expecting another character (or escape char) to terminate the input? I know that console exes can receive stdin from PowerShell. If you have the C# compiler, you can see this by compiling the following source (csc echostdin.cs):
using System;
public class App
{
public static void Main()
{
int ch;
while ((ch = Console.In.Read()) != -1)
{
Console.Write((char)ch);
}
}
}
Then execute the exe:
PS> "foo`r`n" | .\echostdin.exe
foo
PS>