简体   繁体   中英

writing double quotes to file name text with powershell

I have been at great pains to see where I've gone wrong here in adding double quotes to file name text. The following code works fine under the -whatif umbrella but not without.

$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

The desired outcome is to replace 20 board with 20" board . The above snippets give me a rename-item : Illegal characters in path. error.

In Windows you cannot use certain chars in a file or folder name; a double quote is one of those.

Read more at https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file .

Excerpt from that page:

The following reserved characters [are not allowed]:

< (less than)
(greater than)

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

ArcSet's helpful answer contains the crucial pointer: " characters cannot be used in file and folder names on Windows.

There is a - suboptimal - approximation of the desired functionality: you can use a non-ASCII double-quotation mark, which is visually similar - but note that you'll need to use that exact quotation mark to refer to that file later (unless you use wildcards to match it), and the very visual similarity can therefore lead to confusion.

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

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

Note that PowerShell treats regular double quotes and as interchangeable, which is why you still need to ` -escape inside a regular double-quoted string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM