路径带了特殊符号,导致CD命令报错


PowerShell交流中心分类: Powershell基础路径带了特殊符号,导致CD命令报错
0
匿名用户 asked 10年 ago

PS C:\Users\Administrator.WIN-SH746DFKMPA> cd “C:\Users\Administrator.WIN-SH746DFKMPA\Desktop\[eBay]eBay 服务”
cd : 找不到路径“C:\Users\Administrator.WIN-SH746DFKMPA\Desktop\[eBay]eBay 服务”,因为该路径不存在。
所在位置 行:1 字符: 1
+ cd “C:\Users\Administrator.WIN-SH746DFKMPA\Desktop\[eBay]eBay 服务”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\Admini…p\[eBay]eBay 服务:String) [Set-Location], ItemNotFound
E xception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

3 Answers
5
Best Answer
Mooser Lee 管理员 answered 10年 ago

中括号在PowerShell路径中拥有通配符的作用,所以尽量不要使用,如果要使用需要用双转义字符。

mkdir "[xxx]xxx srv"
cd  "``[xxx``]xxx srv"
Mooser Lee 管理员 replied 10年 ago

关于这一现象的描述可以参考:http://technet.microsoft.com/en-us/library/ff730956.aspx搜索关键字:square brackets Is that because of a bug in Windows PowerShell? No; in fact, PowerShell is doing exactly what it was designed to do. If you read our previous tip on using the range operator (and you did read our previous tip on using the range operator, didn’t you?) then you know that PowerShell uses square brackets as a wildcard character: square brackets enable you to search for items that fall within a specified range. For example, do we need a list of all the files whose file names start with the letters a, b, c, d, e, or f? No problem; this command will take care of that for us:

0
Mooser Lee 管理员 answered 10年 ago

已经提示了说路径不存在了,那就肯定不存在了。试试单引号,还有需要确定你复制过来的路径是不是对的。比如空格等。
我这边没有这个问题:

PS D:\> mkdir "d:\Administrator.WIN-SH746DFKMPA\Desktop\[eBay]eBay 服务"


    Directory: D:\Administrator.WIN-SH746DFKMPA\Desktop


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         8/26/2014  10:28 AM            [eBay]eBay 服务


PS D:\> Test-Path "d:\Administrator.WIN-SH746DFKMPA\Desktop\[eBay]eBay 服务"
False
ivanwa replied 10年 ago

刚刚没找到注册入口,直接回复了一封邮件给你,图比较多!主要问题是下面这点:PS D:\> Test-Path “d:\Administrator.WIN-SH746DFKMPA\Desktop\[eBay]eBay 服务”Falsetest-path检测是false的,但是目录创建是可以成功!目录路径正确,使用了单引号也不能使用。在CMD下该目录是可以切换使用的。详情看邮件(没有收到邮件的话跟我说一下)在这个点上,还是觉得有点问题。

Mooser Lee 管理员 replied 10年 ago

以后尽量不要在邮件中直接回复,因为那个邮件只是用来发送邮件的,我一般不会登陆。当然你说了,我还是登陆看了下。感谢详细说明。

ivanwa replied 10年 ago

你帮助了我解决了问题,应该是我感谢你才对!哈哈

0
robo answered 5年 ago

您好 ,我想获取,目录名称中包含有中括号的路径下面的内容。但是获取不到。 不知道怎么搞,请教一下 ,谢谢。

PS C:\Users\Administrator> cd F:\
PS F:\> mkdir "[sdf]sdfdsf"


    目录: F:\


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2018/10/16     15:44                [sdf]sdfdsf


PS F:\> dir "[sdf]sdfdsf"
PS F:\> dir "[sdf]sdfdsf"
PS F:\> Get-ChildItem "[sdf]sdfdsf"
PS F:\> Get-ChildItem "[sdf]sdfdsf"
PS F:\> Get-ChildItem "```[sdf```]sdfdsf"
Get-ChildItem : 找不到路径“F:\`[sdf`]sdfdsf”,因为该路径不存在。
所在位置 行:1 字符: 1
+ Get-ChildItem "```[sdf```]sdfdsf"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (F:\`[sdf`]sdfdsf:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

PS F:\> Get-ChildItem "``[sdf``]sdfdsf"
Get-ChildItem : 找不到路径“F:\`[sdf`]sdfdsf”,因为该路径不存在。
所在位置 行:1 字符: 1
+ Get-ChildItem "``[sdf``]sdfdsf"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (F:\`[sdf`]sdfdsf:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

PS F:\> Get-ChildItem "`[sdf`]sdfdsf"
PS F:\> Get-ChildItem "(sdf)sdfdsf"


    目录: F:\(sdf)sdfdsf


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018/10/16     15:44              0 新建文本文档.txt


PS F:\>
robo replied 5年 ago

最后面小括号,我是测试一下把中括号换成小括号试一下的。

robo replied 5年 ago

解决了,Get-ChildItem -LiteralPath F:\[sdf]sdfdsf

Mooser Lee 管理员 replied 5年 ago

感谢分享!