Hi.
I have a 'small' issue 
I want to execute a command that includes a pipe.
The sqlite3.exe
requires a command line call in the form echo <sql statement> | sqllite3.exe [-json] <sql db location>
.
What would be the syntax to call the ProcessRunner to achieve that?
FWIW, here's the actual code relying on WScript.Shell:
var fso = new ActiveXObject("Scripting.FilesystemObject");
var shell = new ActiveXObject("WScript.Shell");
var tmpFileBase = "DO.Temp.";
var tmpFileExt = ".txt";
var tmpFileStdout = ExtSystem.GetTmpFileName(tmpFileBase, tmpFileExt, fso).fullname;
var tmpFileStderr = tmpFileStdout+".err"+tmpFileExt;
var cmdLine = '%comspec% /c "echo ' + sqlStatement + ' | ';
cmdLine += '"' + sql + '" -json "' + db + '"';
cmdLine += ' >"'+tmpFileStdout+'" 2>"'+tmpFileStderr+'""'
var result = {};
result.cmd = cmdLine;
result.returncode = shell.Run(cmdLine, 0, true);
result.stdout = ExtSystem.ReadFile(tmpFileStdout, fso); fso.DeleteFile(tmpFileStdout);
result.stderr = ExtSystem.ReadFile(tmpFileStderr, fso); fso.DeleteFile(tmpFileStderr);
With ExtSystem defined as follow:
if (typeof ExtSystem === "undefined") {
var ExtSystem = (function () {
var my = {};
///////////////////////////////////////////////////////////////////////////////
my.RunHiddenEx = function RunHiddenEx(exe, params, tmpFileBase, tmpFileExt, shell, fso) { //v1.2
if (!fso) fso = new ActiveXObject("Scripting.FilesystemObject");
if (!shell) shell = new ActiveXObject("WScript.Shell");
if (!tmpFileBase) tmpFileBase = "DO.Temp.";
if (!tmpFileExt) tmpFileExt = ".txt";
var tmpFileStdout = this.GetTmpFileName(tmpFileBase, tmpFileExt, fso).fullname;
var tmpFileStderr = tmpFileStdout+".err"+tmpFileExt;
var cmd = '%comspec% /c ""'+exe+'" '+params+' >"'+tmpFileStdout+'" 2>"'+tmpFileStderr+'""';
//dout ("CMD='" + cmd + "'");
var result = {};
result.cmd = cmd;
if (exe.match(/^([A-z]:\\.+|\\\\(.*?)\\.+)$/)) { //test path to exe if given
if (!fso.FileExists(exe)){
var msg = "E Executable not found ["+exe+"]";
DOpus.Output(msg);
throw msg;
}
//else dout (">> '" + exe + "' exists");
}
result.returncode = shell.Run( cmd, 0, true);
//result.returncode = shell.Run( cmd, 1, true);
result.stdout = this.ReadFile(tmpFileStdout, fso); fso.DeleteFile(tmpFileStdout);
result.stderr = this.ReadFile(tmpFileStderr, fso); fso.DeleteFile(tmpFileStderr);
return result;
}
///////////////////////////////////////////////////////////////////////////////
my.ReadFile = function ReadFile(path, fso){
fso = fso || new ActiveXObject("Scripting.FilesystemObject");
var content = "";
if (!fso.FileExists(path)){
return content;
}
var file = fso.OpenTextFile( path, 1, -2); // Read, UseDefaultEncoding
if (!file.AtEndOfStream)
content = file.ReadAll();
file.Close();
return content;
}
///////////////////////////////////////////////////////////////////////////////
my.ReadFileByLines = function ReadFileByLines(path, fso){
var vOut = DOpus.Create.Vector();
fso = fso || new ActiveXObject("Scripting.FilesystemObject");
var content = "";
if (!fso.FileExists(path)){
return vOut;
}
var file = fso.OpenTextFile(path, 1, false, -2); // Read, do not create, Unicode
while (!file.AtEndOfStream) {
// DOpus.Output("ext system : reading 1 line");
vOut.push_back(file.ReadLine());
}
file.Close();
return vOut;
}
///////////////////////////////////////////////////////////////////////////
my.GetTmpFileName = function GetTmpFileName(prefix, extension, fso) {
fso = fso || new ActiveXObject("Scripting.FilesystemObject");
var tFolder = fso.GetSpecialFolder(2); //2 = temp folder
var tFile = fso.GetTempName();
if (prefix!=undefined) tFile=prefix+tFile;
if (extension!=undefined) tFile+=extension;
return {
path : tFolder.Path,
name : tFile,
fullname: tFolder.Path+'\\'+tFile
};
}
return my;
}());
}
EDIT:
I found a way to avoid the pipe ... there's an alternate syntax in case of a single sql statement (sqlite3.exe [-json] <sql db location> <sql statement>
).
But I have two issues:
- Calling
Run
method leads to a cmd window flashing which was avoided with shell.Run(cmdLine, 0, true);
- The execution time is surprinsgly longer than with previous method : about 1800 ms vs. about 800 before. Could be related to the window "flash" ... but I'm not sure.