我需要将所有命令放在一个带有一些逻辑的批处理文件(test.cmd)中,例如:
IF condition1 (c:\Windows\System32\schtasks.exe /Create ...)
Else (c:\Windows\System32\schtasks.exe /delete ...)
如果删除if-else语句,并且只在test.cmd中保留一个命令,则可以使用如下代码执行该命令:
exec('some-path/test.cmd', (error, stdout, stderr) => {
if (error) {
console.log(error);
return;
}
console.log(stdout);
});
如果添加if-else语句,有人知道如何从node.js exec()函数传递参数吗?在终端中,很容易传递"test.cmd para1“这样的参数。
发布于 2016-08-26 19:48:10
Yo可以使用node spawn。示例变量
const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);
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}`);
});
https://stackoverflow.com/questions/39173106
复制