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"));
  }
});
1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thx. How i can repeat batfile.bat commands x times and repeat opening of batfile.bat?
You can implement that logic inside js itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.