Electron: run shell commands with arguments

14,560

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
});
Share:
14,560

Related videos on Youtube

sigmaxf
Author by

sigmaxf

Updated on October 13, 2022

Comments

  • sigmaxf
    sigmaxf over 1 year

    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?