Split-Path 不喜欢 Leaf 中的冒号

问题描述 投票:0回答:1

tl;博士

叶名中有冒号(

Split-Path
)时,有没有办法用
:
获取父路径?或者有比
Split-Path
更好的解决方案吗?

完整版:

我正在尝试从路径字符串中提取父容器名称。但这不是 Windows 文件系统中的路径。它是在 XML 文档中存储为字符串的路径。从技术上讲,它与系统 (Cognos Analytics) 中的文件夹和内容有关。数据作为文本值存储在 XML 文件(报告规范)中。

我有一些看起来像这样的路径值:

/content/folder[@name='Reports']/folder[@name='Administrative']/folder[@name='Audit']/package[@name='Merchant Code']/model[@name='2015-10-22T22:25:20.362Z']

与其创建我自己的函数来处理这个问题,我想我可以使用

Split-Path

Split-Path (Split-Path $b) -Leaf

但是如果叶子值包含一个冒号(像我的例子):

Split-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
At line:5 char:28
+ "parent name  $(Split-Path (Split-Path $a) -Leaf)"
+                            ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Split-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.SplitPath

这有效:

$a = "/a/b/c/model"
"parent path  $(Split-Path $a)"
"parent path  $(Split-Path $a -Parent)"
"leaf         $(Split-Path $a -Leaf)"
"parent name  $(Split-Path (Split-Path $a) -Leaf)"

输出:

parent path  \a\b\c
parent path  \a\b\c
leaf         model
parent name  c

但是如果我在叶子中添加一个冒号:

$a = "/a/b/c/mod:el"
Split-Path $a
Split-Path $a -Leaf

输出:

Split-Path $a
Split-Path $a -Leaf

mod:el

可以看到,返回叶值没有问题,但是父路径是空字符串(第3行)。如果它是空白的,则会导致错误。

powershell cognos cognos-11
1个回答
0
投票

你在什么操作系统上运行命令?如果您在 Windows 上运行此命令,则该命令失败的原因是冒号字符是无效字符,不能成为路径的一部分。如果您在任何其他操作系统上运行它,如果您尝试像这样转义冒号会发生什么:

$a = "/a/b/c/mod`:el"
© www.soinside.com 2019 - 2024. All rights reserved.