Объедините в PowerShell путь с пробелами, переменную и параметры - PullRequest
0 голосов
/ 10 июля 2019

У меня есть скрипт PowerShell, который работает, когда путь к exe не имеет пробелов, но не работает с пробелами.Как я могу это исправить?

$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
}

1 Ответ

1 голос
/ 10 июля 2019

Используйте оператор вызова &:

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

или с исполняемым путем в переменной:

$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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...