To run a batch file from node i am using below given code and it is working fine.
const { spawn } = require('child_process');
const bat = spawn('cmd.exe', ['/c','D:/somefolder/bin/tools/caprequestutil.bat']);
bat.stdout.on('data', (data) => {
console.log('data is : '+data.toString());
});
bat.stderr.on('data', (data) => {
console.error('error is : '+data.toString());
});
bat.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});
It is working fine. Now i want to pass some parameter to this batch file which is not working for me. I'm trying to simply add the parameters after my .bat file as given below:
const bat = spawn('cmd.exe', ['/c','D/somefolder/bin/tools/caprequestutil.bat -id D:/somefolder/bin/tools/IdentityClient.bin -h 1234-567-8765-4532 -type STRING -attr key value -activate acdvdsfdc count https://someurl/foo -full']);
How can i add these params?
Found the answer.
The trick is to send the parameters in the form of arrays like given below
const bat = spawn('cmd.exe', ['/c',"D/somefolder/bin/tools/caprequestutil.bat", ["-id", "D:/somefolder/bin/tools/IdentityClient.bin"], ["-h", "1234-567-8765-4532"], ["-type", "STRING"], ["-attr", "key","value"], ["-activate", "acdvdsfdc "], ["count "], ["https://someurl/foo"], ["-full"]]);