Carlos Ortega
Carlos Ortega

Reputation: 11

Running multiple command lines in series

I am trying to run a command line, after a command line. The purpose is to go into the correct directory and then create a folder there.

I have looked at the node.js api, although the code looks complicated and it doesnt directly show how to run multiple command line arguments.

const { exec } = require('child_process');
exec(['cd desktop', 'mkdir Folder'], (err) => {
    if (err) {
        console.log(err);
    }
});

I am hoping to go into the "desktop" directory, and then create a folder there. The whole purpose is to run the following two commands in series.

cd desktop
mkdir Folder

Upvotes: 1

Views: 689

Answers (1)

TGrif
TGrif

Reputation: 5941

According to documentation, child_process.exec() takes a string command argument, not an array of commands.

To chain two commands, like in your example, you can do:

const { exec } = require('child_process');

exec('cd desktop && mkdir Folder', (err) => {
  if (err) console.log(err);
});

See also this question if you want to chain more commands sequentially.

Upvotes: 1

Related Questions