2

i am using following code in my app:

var path = app.getAppPath();
var spawn = require('child_process').spawn;
const child = exec(`"${path}\\PC-BASIC\\a.bat"`, []);
var stdout = '';
var stderr = '';
child.stdout.on('data', function(buf) {
  //console.log('[STR] stdout "%s"', String(buf));
  stdout += buf;
});
child.stderr.on('data', function(buf) {
  //console.log('[STR] stderr "%s"', String(buf));
  stderr += buf;
});
child.on('close', function(code) {
  console.log('[END] code', code);
  console.log('[END] stdout "%s"', stdout);
  console.log('[END] stderr "%s"', stderr);
});

when i run a.bat from command line (dos). i get the exact result. but when i run it from my electron app i writes following on console:

 [END] code 0
 [END] stdout "
 D:\Documents\Nauman Umer\New folder\electron-quick-start>"C:\Program Files (x86)\PC-BASIC\pcbasic.com" --load="ART.BAS" --convert=A
 "
 [END] stderr ""

but expected is:

 [END] code 0
 [END] stdout "
 D:\Documents\Nauman Umer\New folder\electron-quick-start>"C:\Program Files (x86)\PC-BASIC\pcbasic.com" --load="ART.BAS" --convert=A
 [FILE TEXT AS IN OUTPUT OF BAT]
 "
 [END] stderr ""

i also tried to execute pcbasic directly from command line instead of from app but the results are same.

2
  • 1
    Are you sure the program prints to stdout. Not all programs on Windows print to stdout. Some directly call the console API to print their output. Commented Sep 26, 2016 at 17:11
  • its an python program and using sys.stdout.write() for writing to console. Commented Sep 27, 2016 at 15:55

1 Answer 1

1

If the child process uses GetConsoleMode to detect whether it runs in a console, try calling it using spawn() rather than exec() in line 3 of your code.

child_process.exec() launches a console, and then launches the program inside that console - so the child process called by a.bat sees a console and may use the console API, as suggested by @slebetman .

child_process.spawn() launches the program directly and makes its stdout available through a pipe, so the child process should not see a console (and cannot use the console API).

See here for details: https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Sign up to request clarification or add additional context in comments.

2 Comments

I am using this code const spawn = require('child_process').spawn; const ls = spawn("D:\\Documents\\Nauman Umer\\New folder\\electron-quick-start\\PC-BASIC\\a.bat", []); ls.stdout.on('data', (data) => { console.log(`stdout: ${data}`); }); ls.stderr.on('data', (data) => { console.log(`stderr: ${data}`); }); ls.on('close', (code) => { console.log(`child process exited with code ${code}`); }); but note getting any thing
the output is stdout: D:\Documents\Nauman Umer\New folder\electron-quick-start>"D:\Documents\Nauman Umer\New folder\electron-quick-start\PC-BASIC\pcbasic.com" --load="ART.BAS" --convert=A child process exited with code 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.