【问题标题】:How to launch cmd by using node.js file?如何使用 node.js 文件启动 cmd?
【发布时间】:2021-06-19 08:41:30
【问题描述】:

如何通过.js文件启动cmd并自动在里面输入一些命令?

const { exec } = require("child_process");
const open = require("open");

const readline = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readline.question("Launch cmd?", async (name) => {
  if (name === "Y") {
    await open("cmd.exe", { wait: true }); //how i can write something in cmd?
  } else {
    process.exit(console.log("You have closed the programm"));
  }
});

【问题讨论】:

标签: javascript node.js


【解决方案1】:

您可以执行与此线程中所述类似的操作。

How to open a command line window in Node.js?

start cmd.exe /K node my-new-script.js parm1 parm2

但是,如果您想运行多个命令,请使用批处理脚本并从 Nodejs 运行该批处理脚本。批处理脚本存储在简单的文本文件中,其中包含带有按顺序执行的命令的行,一个接一个。

require('child_process').exec('cmd /c batfile.bat', function(){
   // …you callback code may run here…
});

您可以在此处了解有关批处理脚本的更多信息。 https://www.tutorialspoint.com/batch_script/batch_script_overview.htm

【讨论】:

  • 谢谢。如何重复 batfile.bat 命令 x 次并重复打开 batfile.bat?
  • 你可以在js内部实现这个逻辑。
猜你喜欢
  • 2017-06-28
  • 1970-01-01
  • 2021-06-04
  • 2019-12-10
  • 2023-03-12
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多