Get-ChildItem 和不间断空间 [英] Get-ChildItem and non-breaking space

查看:76
本文介绍了Get-ChildItem 和不间断空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的文件服务器上工作时,我注意到一个奇怪的文件夹破坏了我的脚本.文件夹的名称仅由一个字符组成,ascii 值为 160(不间断空格,NBSP).从视觉上看,该名称与空格字符相同.

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.

简而言之,我在这个文件夹上执行了 Get-ChildItem,它正在进入无限循环.命令实际上是针对父文件夹执行的,它再次返回有问题的文件夹,因此我的脚本陷入了无限循环.

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.

您可以在自己的环境中轻松地模拟这一点.在 C:\temp 文件夹中创建名称仅包含 NBSP 的新文件夹.您可以通过按住 alt 并在数字键盘上按 0160 来输入.创建之后,运行

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
. . .

我在服务器和客户端操作系统上使用 PowerShell 4 和 5 对此进行了测试,并且具有相同的行为.命令 Get-Item 也有这个名称的问题,并且两个开关 -Path-LiteralPath 的行为方式相同.我也试过 [System.IO.Directory] 类,但它有同样的问题.

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.

问题:我已经更新了我的脚本以将具有此名称的文件夹报告为错误并跳过它,但我想知道有没有更聪明的方法来做到这一点?我的目标是针对此类文件夹运行 Get-ChildItem 或等效项.

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?

推荐答案

如评论中所述,您发现了一个实际错误,希望很快得到修复.

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

但是,有一种非常可接受的解决方法,您可以在继续使用 Get-ChildItem 的同时以最少的努力应用它,而无需排除您的文件夹.

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.

Get-ChildItem 的 Unicode 版本没有这个问题.(在 Windows 10 环境中在 Powershell 5.1 上测试)要使用它,只需替换

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 

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

附加说明

如果需要处理UNC,UNC unicode调用稍有不同.

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 

请注意,我使用 -LiteralPath 参数而不是 -Path 来正常工作.

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

参考资料

来自微软文档

-LiteralPath

指定一个或多个位置的路径.与 -Path 参数不同,-LiteralPath 参数的值与键入的值完全相同.没有字符被解释为通配符.如果路径包含转义字符,请将其括在单引号中.单引号告诉 Windows PowerShell 不要将任何字符解释为转义序列.

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.

来源

关于 unicode 前缀约定:命名文件、路径和命名空间

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

奖金unicode 调用还具有解决 260 个字符路径长度限制的好处:参见此处

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

这篇关于Get-ChildItem 和不间断空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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