windows下node.js调用bat |
您所在的位置:网站首页 › js执行bat › windows下node.js调用bat |
node.js调用bat需要用到Child Processes模块 因为bat是文件,所以需要使用execFile方法
如果指定了cwd,它会切换bat执行的目录,类似cd的功能,如果未指定默认为当前调用程序的目录。如果bat有输出错误,例如创建指定的文件/目录已经存在时,会返回一个错误信息时,调用bat会得到一个相关的错误信息:Error {killed: false, code: 1, signal: null} process.execFile(url, [1, 2], {cwd:'D:/'}, function(error, stdout, stderr) { console.log(error); console.log(stdout); alert(1);});
如果只指定了盘符,而非一个可访问的路径时,会得到Error: spawn EBADF process.execFile(url, [1, 2], {cwd:'D'}, function(error, stdout, stderr) { console.log(error); console.log(stdout); alert(1);}); 输出:Error {code: "EBADF", errno: "EBADF", syscall: "spawn"}![]() child_proc.stdout.on('data', function(data) { console.log(data);}); 假设bat的文件内容是创建三个目录,其中二个目录是通过参数传递进去的(上面代码中的数组[1, 2],其中1、2就是参数) @echo offecho hahaniu~~~ mkdir %1mkdir %2mkdir aa 执行上面的代码后,会在指定的目录下(也就是代码中cwd参数的值)创建相应的目录,其中stdout将得到“hahaniu~~~”的输出 }); ![]() console.log(path);}); ![]() ![]() ![]() |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |