0

I have an array like this:

var array = ['File Name', ...]

I'm executing unix commands like this:

for (const item of array) {
   execSync(`convert -trim ./original/${item}.png ./trimmed/${item}.png`)
}

Here I get an error because unix format spaces like this File\ Name. So, in my code, the script can't file the original file.

What is the usual way to handle this?

4
  • You can surround the name with quotes. It's the usual way to handle spaces in the path. Commented May 18, 2020 at 5:45
  • @VLAZ Could you give me an example? Commented May 18, 2020 at 5:50
  • execSync(`convert -trim "./original/${item}.png" "./trimmed/${item}.png"`) Commented May 18, 2020 at 5:51
  • @VLAZ Damn, the solution was so simple ... could you post this as an answer? Commented May 18, 2020 at 5:53

1 Answer 1

1

To avoid problems with spaces, you can simply surround the full path in double quotes. It's a very common way for the shell to handle spaces in paths:

execSync(`convert -trim "./original/${item}.png" "./trimmed/${item}.png"`)

The other way of handling spaces is to escape them but it gets ugly as you also need to escape the escapes, if you don't need a space. It also makes reading the path really hard when debugging and overall it's probably not worth the hassle.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.