sigmaxf
sigmaxf

Reputation: 8482

Electron: run shell commands with arguments

I'm building an electron app,

I can run shell commands pretty easily with the shell api (https://electronjs.org/docs/api/shell)

This command runs perfect for example:

shell.openItem("D:\test.bat");

This one does not

shell.openItem("D:\test.bat argument1");

How to run electron shell command with arguments?

Upvotes: 18

Views: 30356

Answers (1)

NullDev
NullDev

Reputation: 7303

shell.openItem isn't designed for that.
Use the spawn function of NodeJS from the child_process core module.

let spawn = require("child_process").spawn;

let bat = spawn("cmd.exe", [
    "/c",          // Argument for cmd.exe to carry out the specified script
    "D:\test.bat", // Path to your file
    "argument1",   // First argument
    "argumentN"    // n-th argument
]);

bat.stdout.on("data", (data) => {
    // Handle data...
});

bat.stderr.on("data", (err) => {
    // Handle error...
});

bat.on("exit", (code) => {
    // Handle exit
});

Upvotes: 22

Related Questions