为变量添加双引号以转义空格 [英] Add double quotes to variable to escape space

查看:63
本文介绍了为变量添加双引号以转义空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个脚本,其中 $FileName 变量包含带空格的绝对路径.由于目录名和文件名中的空格,脚本在没有找到实际路径的情况下无法执行.我只需要在双引号内添加 $FilePath .我应该如何在字符串的开头和结尾附加双引号?

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 executes without finding the actual path. All I need is to add $FilePath within double quotes. How should I append double quotes in the beginning and end of a string?

例如

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

脚本:

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

电流输出:

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

推荐答案

除了反引号转义符(`),还可以使用-f格式操作符:

In addition to the backtick escape character (`), you can use the -f format operator:

$FilePath = Join-Path $Dir -ChildPath "$File.txt"
$FilePathWithQuotes = '"{0}"' -f $FilePath

这将确保 $FilePath 在放入字符串之前被扩展

This will ensure that $FilePath is expanded before being placed in the string

这篇关于为变量添加双引号以转义空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆