Powershell 中的路径和字符串切割
我正在做一些涉及一些自动文件移动的工作,这些文件包含必须维护的相对路径。不幸的是,我发现 System.IO.Path、System.String 和 Powershell 操作员提供的设施有点不足以优雅地处理我的工作。
对我来说非常有用的一个函数是路径减法的概念,理论上它就像减去向量一样。从概念上讲,A - B 为您提供从 B 到 A 的路径。在路径应用程序中,D:\A\B\C\D - D:\A\B\ = \C\D
。同样,在本例中,D:\A\B\ - D:\A\B\C\D = \..\..
。目前我可以接受,只有当一条路径完全包含在另一条路径中时,这才有意义。
这似乎由两个步骤组成:1) 确定一条路径包含在另一条路径中。 2) 从包含路径中删除包含路径。 3) (可选)根据操作的侧面,将文件夹名称替换为父 .. 符号。
由于我关心 NTFS,因此我需要包含和替换操作不区分大小写。为了遏制,我可以使用 select-string 因为它不区分大小写,并且允许使用 -simple 开关,这允许我使用路径而无需将其分开以将其转义为正则表达式。
不过,从另一个身上取下绳子有点烦人。 System.IO.Path对此没有任何帮助,System.String的相关方法都是区分大小写的,并且powershell的运算符都需要进行按摩,以便正则表达式能够匹配事物。
所有这些似乎比应有的工作量要多——我是否缺少任何工具可以更好地处理这个问题?
I'm doing some work involving some automated file moving, and these files contain relative paths that must be maintained. Unfortunately, I'm finding the facilities offered by System.IO.Path, System.String, and Powershell's operators to be a little ill-equipped to handle my work gracefully.
One function that would be very useful to me is the notion of a subtraction of paths, that would work in theory like subtracting vectors. Conceptually, A - B gets you a path from B to A. In the application to paths, D:\A\B\C\D - D:\A\B\ = \C\D
. Likewise, D:\A\B\ - D:\A\B\C\D = \..\..
in this case. I can accept, for now, that this only makes sense when one path is wholly contained in the other.
This seems to consist of two steps: 1) determine containment of one path in the other. 2) remove the contained path from the containing path. 3) Optionally, replace folder names with the parent .. symbol based on the sidedness of the operation.
As I am concerned with NTFS, I need both containment and replacement operations to be case-insensitive. For containment, I can use select-string since it is case-insensitive, and allows the -simple switch which allows me to use a path without hacking it apart to escape them for regex.
Removing the string from the other is a little more annoying though. System.IO.Path has nothing for this, System.String's pertinent methods are all case-sensitive, and powershell's operators all require massaging so that the regex will match things.
All this seems like more work than it should be--are there any tools I'm missing that would better handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确定包含 - 将您的路径转换为绝对路径(如果还没有)。您可以使用 Resolve-Path 来实现此目的。然后,您可以使用 $path1.StartsWith($path2, 'OrdinalIgnoreCase') 来测试包含。
删除包含的路径 - $path1.Substring($path2.length)
将父文件夹名称替换为 ... - 尽管我不这样做我不知道正则表达式,我很确定您可以使用 PowerShell 的 -replace 运算符通过正则表达式搜索/替换来完成此操作
Determine containment - convert your paths to absolute paths (if not already). You can use Resolve-Path for this. Then you can use $path1.StartsWith($path2, 'OrdinalIgnoreCase') to test for containment.
Remove contained path - $path1.Substring($path2.length)
Replace parent folder names with ... - although I don't have the regex off the top of my head, I'm pretty sure you could do this with a regular expression search/replace using PowerShell's -replace operator
CodePlex 上的 filedirectorypath 可能会提供您所需的内容
它不是 PowerShell 特定的 API,但这没有理由不使用来自 PowerShell。
NDepend.Helpers.FilePathDirectory 相对于 .NET Framework 类 System.IO.Path 的优点包括:
filedirectorypath, on CodePlex, may offer what you need
It's not a PowerShell specific API, but that's no reason not to use it from PowerShell.
Benefits of the NDepend.Helpers.FilePathDirectory over the .NET Framework class System.IO.Path include: