使用PowerShell将双引号写入文件名文本

时间:2018-10-03 01:07:47

标签: powershell filenames illegal-characters

在为文件名文本添加双引号时,我很想知道我在哪里出错了。以下代码在-whatif框架下可以正常工作,但不能没有。

$a = "20`" board"
get-childitem | rename-item -newname {$_.fullname -replace "20 board", $a } -whatif

get-childitem | rename-item -newname {$_.fullname -replace "20 board","20`" board" } -whatif

期望的结果是将20 board替换为20" board。 以上代码段给我一个rename-item : Illegal characters in path.错误。

2 个答案:

答案 0 :(得分:3)

在Windows中,您不能在文件或文件夹名称中使用某些字符; 双引号就是其中之一。

https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file上了解更多信息。

该页面的摘录:

The following reserved characters [are not allowed]:

< (less than)
(greater than)

: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

答案 1 :(得分:1)

ArcSet's helpful answer包含关键指针:"字符不能在Windows的文件和文件夹名称中使用。

所需功能有-次优-逼近:您可以使用非ASCII 双引号,在视觉上相似。 em>-但请注意,以后您需要使用精确的引号引用该文件(除非您使用通配符将其匹配),因此非常相似的视觉外观可能导致混淆。

'hi' > "20`“ board" # works, because “ is not a regular double quote

Unicode character U+201C, the LEFT DOUBLE QUOTATION MARK

请注意,PowerShell将常规双引号和视为可互换,这就是为什么您仍然需要在常规双引号字符串内`-转义的原因。

相关问题