Get-ChildItem and non-breaking space

扶醉桌前 提交于 2020-01-24 02:01:28

问题


While working on my file servers, I have noticed one strange folder that broke my script. Folder has name consisting of only one character with ascii value 160 (non-breaking space, NBSP). Visually that name is the same as space character.

Briefly, I have Get-ChildItem being executed on this folder and it is entering endless loop. Command is in reality executed against parent folder and it returns again problematic folder, so my script got into endless loop.

You can easily simulate this on your own environment. In C:\temp folder create new folder whose name consists only of NBSP. You can type that with holding alt and pressing 0160 on numerical keyboard. After creating it, run

Get-ChildItem C:\Temp\ -Recurse

you will get endless list of folders without name, although I have only that one folder.

d-----        6/15/2017   2:20 PM
d-----        6/15/2017   2:20 PM
d-----        6/15/2017   2:20 PM
d-----        6/15/2017   2:20 PM
d-----        6/15/2017   2:20 PM
. . .

I tested this with PowerShell 4 and 5, on Server and Client OS and its the same behavior. Command Get-Item is having also issue with this name, and both switches -Path and -LiteralPath are behaving in the same way. I tried also [System.IO.Directory] class, but it was having the same issue.

Question: I have updated my script to report folders with this name as an error and skip it, but I am wondering is there some more smart way to do it? My goal would be to run Get-ChildItem or equivalent against such folder.

Is this a known issue? Would be be worth reporting it somewhere as a bug, if confirmed by more people?


回答1:


As mentioned in the comment, you found an actual error that will hopefully soon being fixed.

However, there is a very acceptable workaround that you can apply with minimal effort while continuing to use Get-ChildItem without the need to exclude your folder.

The Unicode version of Get-ChildItem does not suffer from this problem. (Tested on Powershell 5.1 on a Windows 10 environment) To use it, simply replace

Get-ChildItem  -Path 'c:\__tmp' -recurse 

by

Get-ChildItem  -LiteralPath '\\?\c:\__tmp' -recurse 

Additional note

If you need to deal with UNC, the UNC unicode call is slightly different.

Get-ChildItem  -LiteralPath '\\?\UNC\127.0.0.1\c$\__tmp' -recurse 

Notice that I use for this to work properly the -LiteralPath parameter instead of -Path.

References

From Microsoft documentation

-LiteralPath

Specifies a path to one or more locations. Unlike the -Path parameter, the value of the -LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.

source

Regarding the unicode prefix convention: Naming Files, Paths, and Namespaces

Bonus The unicode call also have the benefit of solving the 260 characters path length limit : see here



来源:https://stackoverflow.com/questions/44567844/get-childitem-and-non-breaking-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!