0

I have a Node.js program where I need to, on a button click, run 2 commands in the Windows command line. For example, the process I'm trying to automate by the button click would be doable manually by going to cmd and entering the following commands:

pushd \\myserver.com\folder1\folder2         //Connect to remote server folder structure
mkdir NewFolder                              //Create new folder in the remote folder

I've found many resources pointing that I should use 'child_process', but I'm absolutely lost when it comes to shell scripting and am having a really hard time figuring out how to do this. Here's the code I have so far:

var cp = require('child_process');
cp.exec('pushd \\\\myserver.com\\folder1\\folder2\\', { shell: '/bin/bash' }, function(err, stdout, stderr){
    if(err){
        console.log(err);        
    }
});

But this above code just returns this error (which oddly removes the '\'s from the given dir):

{ Error: Command failed: pushd \\myserver.com\folder1\folder2\
/bin/bash: line 1: pushd: \myserver.comfolder1folder2: No such file or directory
    at ChildProcess.exithandler (child_process.js:281:12)

  killed: false,
  code: 1,
  signal: null,
  cmd: 'pushd \\\\myserver.com\\folder1\\folder2\\' }
/bin/bash: line 1: pushd: \myserver.comfolder1folder2: No such file or directory

I'm really lost here and would appreciate any help. Any alternative you have to child_process may also be very helpful. Thank you!

mmarion
  • 859
  • 8
  • 20
  • what bash are you using? this really has nothing to do with node or javascript. also `exec` will drop the context of the new working directory so your `mkdir` 2nd command won't work. you should just consider doing it with typical file system calls, not shelling out. – Daniel A. White Feb 17 '21 at 22:18
  • @DanielA.White Could you elaborate on 'typical file system calls'? If you mean something like fs.mkdir() I've come to the conclusion there's no way to use fs within a remote server and that it will only work locally. That's actually what's leading me to try this cmd way. Any suggestions? I've been stuck on this issue for a few weeks now – mmarion Feb 17 '21 at 22:23
  • you might want to mount the share as a volume then do `fs.mkdir`. – Daniel A. White Feb 17 '21 at 22:44
  • bash does not understand unc paths – Daniel A. White Feb 17 '21 at 22:44

2 Answers2

0

Usually all the escape sequences used for string uses '\\' for a single backslash. It is understandable you used it here for the directory path for windows.

In JS particularly '\\' doesn't exactly work like that

'abc\
def' == 'abcdef' // true

'\a' == 'a' // true

When a '\' is not followed by a character with any special meaning, it is considered to be a LineContinuation instead.

As you can see from your error output using '\\\\myserver.com' considered '\myserver.com'. Plain workaround is to use '\\\\' for single '\' or use '/' for path separation which I'm not pretty sure if shell will execute it. This is one of the blogs explains about it in details Link.

Yogesh C K
  • 34
  • 3
0

The shell is incorrect here:

{ shell: '/bin/bash' }

It should be:

{ shell: 'CMD.EXE' }

Because in the beginning of your post, you tell that you run 2 commands in the Windows command line, which indicates you are not using Bash, which is (usually) not installed on Windows.

starball
  • 20,030
  • 7
  • 43
  • 238