0

I am trying to run a batch file in Electron using a network path. The function I am using is:

    function cpSixHundred() {
  require('child_process').exec("file:\\\\LSC-SA-NAS1\\Departments\\Information Technology\\Software\\Zebra Label Printer Reset Counters", function(err, stdout, stderr) {
    if (err) {
      // Ooops.
      // console.log(stderr);
      return console.log(err);
    }

    // Done.
    console.log(stdout);
  });
}

The error I get is:

Error: Command failed: \\LSC-SA-NAS1\Departments\Information 
Technology\Software\Zebra Label Printer Reset Counterstest.bat
'\\LSC-SA-NAS1\Departments\Information' is not recognized as an internal or 
external command,
operable program or batch file.

I understand it does not like the space in the network path. I have tried many combinations of quotes and string concatenations, but still no luck.

Thank you in advance.

RDumais
  • 171
  • 8
  • 1
    This question looks similar. Maybe not, but worth a look: https://stackoverflow.com/questions/16395612/unable-to-execute-child-process-exec-when-path-has-spaces – Aaron Jun 21 '17 at 19:02

1 Answers1

1

You need to use single quote string and put double quotes around your file path.

One exemple of this taken from node js documentation :

https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

exec('"/path/to/test file/test.sh" arg1 arg2');
//Double quotes are used so that the space in the path is not interpreted as
//multiple arguments

EDIT:

if you can, avoid requiring module in function call, it can slow down your application and it's a bad practice most of the time.

// put all required modules on top of your page
var childProcess = require('child_process');

function cpSixHundred() {
   childProcess.exec('"file:\\\\LSC-SA-NAS1\\Departments\\Information Technology\\Software\\Zebra Label Printer Reset Counters"', function(err, stdout, stderr) {
      if (err) {
         // Ooops.
         // console.log(stderr);
         return console.log(err);
      }

      // Done.
      console.log(stdout);
   });
}
Gatsbill
  • 1,760
  • 1
  • 12
  • 25