1

How to launch cmd by .js file and automatically type some commands in it?

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"));
  }
});
Behemoth
  • 5,389
  • 4
  • 16
  • 40
ITWolf
  • 21
  • 2
  • 1
    https://stackoverflow.com/questions/31737743/how-to-open-a-command-line-window-in-node-js – mathan Jun 19 '21 at 08:54

1 Answers1

0

You can either do something similar as said in this thread.

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

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

But if you want to run multiple commands, use a batch script and run that batch script from Nodejs. Batch Scripts are stored in simple text files containing lines with commands that get executed in sequence, one after the other.

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

You can learn more about the batch script here. https://www.tutorialspoint.com/batch_script/batch_script_overview.htm

Akhil Ravindran
  • 126
  • 1
  • 10