【问题标题】:PowerShell - Removing folder from path of stringPowerShell - 从字符串路径中删除文件夹
【发布时间】:2022-01-12 22:49:25
【问题描述】:

基本上我有一条路 "root/main/EVILFOLDER/first/second/third/etc" 我想删除 EVILFOLDER/ 特别是路径的第三段。问题是 EVILFOLDER 可以是任何东西,所以我不能只是硬编码它,路径长度和“/”的数量可以是任何不同的长度。

我有这个,它可以工作,但它不是优雅的。

$path = "root/main/EVILFOLDER/first/second/third/etc"
$basePath = "{0}/{1}/" -f $path.Split('/')
$subFolder = "{3}/" -f $shortPath.Split('/')
$subFolderIndex = $shortPath.IndexOf($subFolder)
$newPath = $basePath + $shortPath.Substring($subFolderIndex)

最终,我希望我可以将它写成类似于“{0}/{1}/{3}...”的内容来获取路径列表的其余部分。想法?

【问题讨论】:

    标签: string powershell replace substring


    【解决方案1】:

    试试这个:

    'root/main/EVILFOLDER/first/second/third/etc',
    'root/main/EVILFOLDEREVILFOLDEREVILFOLDER/first/',
    'root/main/EVILFOLDEREVILFOLDEREVILFOLDER',
    'root/main/zzz/first/second/third/etc',
    'root/main/tinyEvil/aaaa/bbbb/cccc/dddd/eee' | ForEach-Object {
        $good1, $good2, $notgood, $good3 = $_ -split '(?=/)',4
        $newName = -join( $good1, $good2, $good3 )
    
        [PSCustomObject]@{
            OldName = $_
            NewName = $newName
        }
    }
    
    # Results
    
    OldName                                         NewName
    -------                                         -------
    root/main/EVILFOLDER/first/second/third/etc     root/main/first/second/third/etc
    root/main/EVILFOLDEREVILFOLDEREVILFOLDER/first/ root/main/first/
    root/main/EVILFOLDEREVILFOLDEREVILFOLDER        root/main
    root/main/zzz/first/second/third/etc            root/main/first/second/third/etc
    root/main/tinyEvil/aaaa/bbbb/cccc/dddd/eee      root/main/aaaa/bbbb/cccc/dddd/eee
    

    基本上我们关心的是保留$good1$good2,它可能是也可能不是$good3$notGood 之后,但这应该能够处理它。

    【讨论】:

    • 那么-split '(?=/)',4 是一种获取$good3 中所有内容的方法吗?即` $newpath = "{0}/{1}/{3}" -f $path.Split('?=/',4) `
    • @DavidAustin 只要路径有 4 个或更多段,就会有一个 $good3。如果路径有 3 个段,该变量将是 null,例如我的答案 root/main/EVILFOLDEREVILFOLDEREVILFOLDER 中的那个只有 3 个段,它被转换为 root/main
    • $newPath = "{0}/{1}/{3}" -f $shortPath.Split('?=/',4) 这就是我最终的结果。
    • @DavidatFishtank 我已经编辑了我的答案以帮助您进行比较,也许可以让您了解我的意思。
    • @DavidatFishtank 请注意,.split(..) 不理解 regex?=/ 的字面意思,使用字符串格式可能会导致您使用它的方式出错。
    【解决方案2】:

    -split 很好,但是这个正则表达式也可以解决问题:

    $NewPath = $Path -replace "^([^/]+/[^/]+)/[^/]+(.*)", '$1$2'
    

    ^ 字符串开头

    ([^/]+/[^/]+) 第一个要保留的块,[^/]+ 将保留任何字符(至少 1 个)但 /,因此这对应于“任何字符/任何字符”

    /[^/]+ 要删除的字符(“/除/ 之外的任何字符”)

    (.*) 要保留的第二个块:“任何字符(可以是无字符)”

    替换为$1$2对应的blocks以保留1和2

    如果您的路径使用反斜杠,那么您需要在正则表达式中转义它们:^([^\\]+\\[^\\]+)\\[^\\]+(.*)

    【讨论】:

      【解决方案3】:
      $data = 'root/main/EVILFOLDER/first/second/third/etc' -split "/";
      $data[0,1]+$data[3..$data.length] -join "/"
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 2013-10-10
      • 2019-03-04
      • 2017-12-18
      相关资源
      最近更新 更多