1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.