简体   繁体   English

对路径中的字符进行转义的函数

[英]Function to escape characters in paths

Is there a function in PowerShell for escaping characters in paths? PowerShell 中是否有用于对路径中的字符进行转义的函数?

NB: I'm aware that most cmdlets which provide a Path parameter also provide a LiteralPath parameter which resolves this issue.注意:我知道大多数提供Path参数的 cmdlet 也提供了解决此问题的LiteralPath参数。 This question is more from curiosity than need, as in most use cases I can think of, switching to LiteralPath makes sense.这个问题更多是出于好奇而不是需要,因为在我能想到的大多数用例中,切换到LiteralPath是有道理的。 However there are some genuine use-cases (eg Start-BitsTransfer has a Source parameter, but no literal equivalent).然而,有一些真正的用例(例如Start-BitsTransfer有一个Source参数,但没有文字等效)。

Detail详情

If I have a file c:\\temp\\test[0123].txt , instead of Get-Item 'c:\\temp\\test[0123].txt' , I'd have to use Get-Item 'c:\\temp\\test`[0123`].txt' to get a result (or make use of the LiteralPath parameter).如果我有一个文件c:\\temp\\test[0123].txt ,而不是Get-Item 'c:\\temp\\test[0123].txt' ,我必须使用Get-Item 'c:\\temp\\test`[0123`].txt'以获得结果(或使用LiteralPath参数)。

Even the path returned by another PowerShell command returns an unescaped string;即使是另一个 PowerShell 命令返回的路径也会返回一个未转义的字符串; ie Get-ChildItem 'c:\\temp\\' -Filter 'test*.txt' | Convert-Path | Get-ItemGet-ChildItem 'c:\\temp\\' -Filter 'test*.txt' | Convert-Path | Get-Item Get-ChildItem 'c:\\temp\\' -Filter 'test*.txt' | Convert-Path | Get-Item Get-ChildItem 'c:\\temp\\' -Filter 'test*.txt' | Convert-Path | Get-Item fails (NB: if we pass the actual FileSystemInfo object all works, but that object has no properties with the correctly escaped string path). Get-ChildItem 'c:\\temp\\' -Filter 'test*.txt' | Convert-Path | Get-Item失败(注意:如果我们传递实际的FileSystemInfo对象一切正常,但该对象没有正确转义字符串路径的属性)。

We can easily escape this using code such as below:我们可以使用如下代码轻松转义:

$path = 'c:\temp\test[0123].txt'
$path = $path -replace '([][[])', '`$1' # escape square brackets by adding back ticks
Get-Item $path

However when escaping strings, standard advice is to avoid rolling your own solution / to make use of the language's solutions to these issues.但是,在转义字符串时,标准建议是避免滚动您自己的解决方案/使用该语言的解决方案来解决这些问题。

Is there any pre-existing function in PowerShell for this purpose, or any recommended way of approaching this;为此,PowerShell 中是否有任何预先存在的功能,或任何推荐的方法来解决这个问题; or is the only option to use a bespoke function (eg below)?或者是使用定制功能的唯一选择(例如下面)?

function ConvertFrom-LiteralPath {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$LiteralPath
    )
    process {
        (New-Object -TypeName 'PSObject' -Property @{
            LiteralPath = $LiteralPath
            Path = $LiteralPath -replace '([][[\*\?])', '`$1' # escapes ], [, *, and ?
        })
    }
}

Info on special characters:特殊字符的相关资料:

There is the following method you can use:您可以使用以下方法:

[Management.Automation.WildcardPattern]::Escape('test[1].txt')

Returns:返回:

test`[1`].txt

Documented here:记录在这里:
https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.wildcardpattern.escape https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.wildcardpattern.escape

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM