5

In perl if you need to run a batch file it can be done by following statement.

system "tagger.bat < input.txt > output.txt";

Here, tagger.bat is a batch file, input.txt is the input file and output.txt is the output file.

I like to know whether it is possible to do it be done in Node.js or not? If yes, how?

1 Answer 1

4

You will need to create a child process. Unline Python, node.js is asynchronous meaning it doesn't wait on the script.bat to finish. Instead, it calls functions you define when script.bat prints data or exists:

// Child process is required to spawn any kind of asynchronous process
var childProcess = require("child_process");
// This line initiates bash
var script_process = childProcess.spawn('/bin/bash',["test.sh"],{env: process.env});
// Echoes any command output 
script_process.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});
// Error output
script_process.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});
// Process exit
script_process.on('close', function (code) {
  console.log('child process exited with code ' + code);
});

Apart from assigning events to the process, you can connect streams stdin and stdout to other streams. This means other processes, HTTP connections or files, as shown below:

// Pipe input and output to files
var fs = require("fs");
var output = fs.createWriteStream("output.txt");
var input =  fs.createReadStream("input.txt");
// Connect process output to file input stream
script_process.stdout.pipe(output);
// Connect data from file to process input
input.pipe(script_process.stdin);

Then we just make a test bash script test.sh:

#!/bin/bash
input=`cat -`
echo "Input: $input"

And test text input input.txt:

Hello world.

After running the node test.js we get this in console:

stdout: Input: Hello world.

child process exited with code 0

And this in output.txt:

Input: Hello world.

Procedure on windows will be similar, I just think you can call batch file directly:

var script_process = childProcess.spawn('test.bat',[],{env: process.env});
Sign up to request clarification or add additional context in comments.

9 Comments

at line number 2 : it is showing unexpected token on var
Ok, try it now. First try just this: spawn('test.bat < input.txt > output.txt');. If it doesn't create output file, try the streams.
now it is showing unexpected token at the opening braces.
@ShvetChakra Ok, I actually tested this on linux and got it to work out of curiosity, hopefully it will help someone else, who can at least copy and paste a script in his IDE.
thanks for your help but you just broke my confidence, its my first day on Node.js and i have never worked on it but i am learning and i will.
|

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.