EPurpl3
EPurpl3

Reputation: 752

Combine in PowerShell a path with spaces and a variable and parameters

I have a PowerShell script that works when the path to the exe doesnt have spaces but doesnt work with spaces. How can i fix this?

$dir = "path/to/directory"
$images = Get-ChildItem $dir
foreach ($img in $images) {
  $outputName = $img.DirectoryName + "\" + $img.BaseName + ".webp"

  ##### The line below works well when there are no spaces
  ##### C:\webp-converter\libwebp-0.6.1-windows-x64\bin\cwebp.exe $img.FullName -o $outputName

  ##### How do i change the syntax to make the line below work?
  C:\Program Files\a folder with many spaces in the title\bin\cwebp.exe $img.FullName -o $outputName
}

Upvotes: 0

Views: 1369

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

Use the & call operator:

& 'C:\Program Files\a folder with many spaces in the title\bin\cwebp.exe' $img.FullName '-o' $outputName

or with the executable path in a variable:

$dir = "path/to/directory"
$images = Get-ChildItem $dir
$exe = "C:\Program Files\a folder with many spaces in the title\bin\cwebp.exe"
foreach ($img in $images) {
  $outputName = Join-Path $img.DirectoryName ($img.BaseName + ".webp")

  & $exe $img.FullName '-o' $outputName
}

Upvotes: 1

Related Questions