gpt4 book ai didi

powershell - 如何在 PowerShell 中访问包含特殊字符的文件路径

转载 作者:行者123 更新时间:2023-12-03 17:07:26 25 4
gpt4 key购买 nike

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

powershell (Get-Item 'C:\Users\erlpm\Desktop\Temp\s p a c e s.txt').creationTime
我将文件路径用单引号括起来,因为它可能包含空格。
它工作正常,直到我遇到包含方括号的文件路径,这似乎被解释为通配符。我设法通过添加 -literalPath 参数来解决它:
powershell (Get-Item -literalpath 'C:\Users\erlpm\Desktop\Temp\brackets[].txt').creationTime
到目前为止,一切都很好……但是文件路径也可能包含单引号、美元符号、与号等,并且所有这些字符在 PowerShell 中似乎都有一个特定的功能,-literalPath 参数似乎对其不起作用。
我尝试用双引号括起路径或用`字符转义,但这也没有解决我的问题:-(
关于如何将文件路径传递给 PowerShell 的任何建议,其中可能包含空格、单引号、方括号、与号、美元符号等?
这里有人已经向我展示了如何在 PowerShell 中使用它,但不知何故,答案已被删除(?)。
无论如何,我确实创建了一个名为 $ & ' [].txt 的文件。 .
这在 PowerShell 中有效(需要转义 & ):
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 命令时,...
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命令放在 {}之间给出完全相同的错误信息......
powershell -command {Get-Item -LiteralPath "C:\Users\erlpm\Desktop\Temp\`$ & ' [].txt"}

最佳答案

如果您可以将路径写入临时 .txt 文件,则以下工作正常:

(Get-Item -literalpath (gc 'C:\Temp\path.txt')).creationTime
文件@ C:\Temp\path.txt 包含其中包含特殊字符的路径,除此之外,我认为您必须在每个路径的基础上对每个特殊字符进行转义。
除了上面的 hack 之外,PowerShell V3 似乎可以通过添加新的“魔法参数”语言功能来提供帮助。具体来说,请参阅标题为“更轻松地重用 Cmd.exe 中的命令行”的部分 here因为我讨厌链接腐烂,所以在这里,无耻地转载如下:

Easier Reuse of Command Lines From Cmd.exe

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

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

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

关于powershell - 如何在 PowerShell 中访问包含特殊字符的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12090312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com