Add double quotes to variable to escape space

后端 未结 4 1677
遇见更好的自我
遇见更好的自我 2020-12-20 22:02

I am running a script which has $FileName variable containing the absolute path with spaces. Due to the space within the directory name and file name, the script fails to ex

4条回答
  •  情深已故
    2020-12-20 22:39

    $FilePath = Join-Path $Path $($Dir + "\" + $File + “.txt”)
    "`"$FilePath`""
    

    ...would output...

    "X:\Movies\File One\File One.txt"
    

    This is an example of variable expansion in strings.

    Of course, if the path you want to quote could contain " quotes itself, for example in a future "powershell for linux", you'd need to escape the " in a context specific way.

提交回复
热议问题