如何访问包含特殊字符的 Powershell 中的文件路径? [英] How to access file paths in Powershell containing special characters?

查看:84
本文介绍了如何访问包含特殊字符的 Powershell 中的文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Java 应用程序中调用 powershell(通过 Windows 命令提示符)来读取各种文件属性.

I am calling powershell from within a Java application (thru the Windows command prompt) to read various file attributes.

例如

powershell (get-item 'C:\Users\erlpm\Desktop\Temp\s p a c e s.txt').creationTime

我将文件路径用单引号括起来,因为它可能包含空格.它工作正常,直到我遇到包含方括号的文件路径,这似乎被解释为通配符.我设法通过添加 -literalPath 参数来解决它:

I am enclosing the file path in single quotes, because it may contain spaces. It worked fine, until I encountered a file path containing square brackets, which seem to be interpreted as a wildcard character. I managed to solve it by adding the -literalPath parameter :

powershell (get-item -literalpath 'C:\Users\erlpm\Desktop\Temp\brackets[].txt').creationTime

到目前为止,一切都很好......但是文件路径也可能包含单引号、美元符号、与号……并且所有这些字符在 powershell 中似乎都有特定的功能,-literalPath 参数似乎对其不起作用.我尝试用双引号括起路径或用`字符转义,但这也没有解决我的问题:-(

So far, so good ... But file paths may also contain single quotes, dollar signs, ampersands, ... and all these characters seem to have a specific function in powershell for which the -literalPath parameter does not seem to work. I tried enclosing the path with double quotes or escaping with the `character, but that did not solve my problem either :-(

有关如何将文件路径传递给 Powershell 的任何建议,其中可能包含空格、单引号、方括号、与号、美元符号等.

Any suggestions on how to pass a file path to Powershell which may contain spaces, single quotes, square brackets, ampersands, dollar signs, etc. ?

更新:这里有人已经向我展示了如何在 Powershell 中使用它,但不知何故,答案已被删除?
无论如何,我确实创建了一个名为 $ & 的文件.' [].txt.这在 Powershell中起作用(需要转义&):

Update: Someone here already showed me how to get it working from within Powershell, but somehow the answer has been removed ?
Anyway, I did create a file called $ & ' [].txt. This works form within Powershell (needed to escape the &) :

    PS C:\Users\erlpm> Get-Item -LiteralPath "C:\Users\erlpm\Desktop\Temp\`$ & ' [].txt"


    Directory: C:\Users\erlpm\Desktop\Temp


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2012-08-23     14:22          0 $ & ' [].txt

但是当我通过 Windows 命令提示符执行相同的 Powershell 命令时,...

But when I execute the same Powershell command thru the Windows command prompt, ...

C:\Users\erlpm>powershell Get-Item -LiteralPath "C:\Users\erlpm\Desktop\Temp\`$ & ' [].txt"

...我收到此错误:

    Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
At line:1 char:55
+ Get-Item -LiteralPath C:\Users\erlpm\Desktop\Temp\`$ & <<<<  ' [].txt
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmpersandNotAllowed

使用 -command 参数并将 powershell 命令放在 {} 之间给出完全相同的错误消息...

Using the -command parameter and putting the powershell command between {} gives exactly the same error message ...

C:\Users\erlpm>powershell -command {Get-Item -LiteralPath "C:\Users\erlpm\Desktop\Temp\`$ & ' [].txt"}

推荐答案

如果你能写一个临时txt文件的路径,下面就OK了:

If you can write the path to a temporary txt file, the following works OK:

(get-item -literalpath (gc 'C:\Temp\path.txt')).creationTime

文件@ C:\Temp\path.txt 包含包含特殊字符的路径,除此之外,我认为您必须在每个路径的基础上对每个特殊字符进行转义.

The file @ C:\Temp\path.txt contains the path with special characters in it, other than this I think you would have to escape each special character on a per path basis.

除了上面的 hack 之外,Powershell V3 似乎可以通过添加新的魔法参数"语言功能来提供帮助.具体来说,请参阅标题为从 Cmd.exe 更轻松地重用命令行"的部分 这里 并且因为我讨厌链接腐烂,所以在这里,无耻地转载如下:

In addition to the hack above, it appears Powershell V3 may help out here with the addition of a new 'magic parameter' language feature. Specifically, see the section headed 'Easier Reuse of Command Lines From Cmd.exe' here and because I hate link-rot, here it is, shamelessly reproduced below:

从 Cmd.exe 中更轻松地重用命令行

网络上充斥着为 Cmd.exe 编写的命令行.这些命令行在 PowerShell 中经常工作,但是当它们包含某些字符,例如分号 (;) 美元符号 ($) 或花括号,您必须进行一些更改,可能会添加一些引号.这似乎是许多轻微头痛的根源.

The web is full of command lines written for Cmd.exe. These commands lines work often enough in PowerShell, but when they include certain characters, e.g. a semicolon (;) a dollar sign ($), or curly braces, you have to make some changes, probably adding some quotes. This seemed to be the source of many minor headaches.

为了帮助解决这种情况,我们添加了一种新方法来逃避"命令行解析.如果你使用魔法参数--%,我们就停止我们正常解析您的命令行并切换到更多内容更简单.我们不匹配引号.我们不会止步于分号.我们不要扩展 PowerShell 变量.我们确实扩展了环境变量如果您使用 Cmd.exe 语法(例如 %TEMP%).除此之外,该直到行尾(或管道,如果是管道)的参数是按原样通过.下面是一个例子:

To help address this scenario, we added a new way to "escape" the parsing of command lines. If you use a magic parameter --%, we stop our normal parsing of your command line and switch to something much simpler. We don’t match quotes. We don’t stop at semicolon. We don’t expand PowerShell variables. We do expand environment variables if you use Cmd.exe syntax (e.g. %TEMP%). Other than that, the arguments up to the end of the line (or pipe, if you are piping) are passed as is. Here is an example:

echoargs.exe --% %USERNAME%,this=$something{weird}
Arg 0 is <jason,this=$something{weird}>

这篇关于如何访问包含特殊字符的 Powershell 中的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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