首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell -需要检查名称是否以符号结尾

Powershell -需要检查名称是否以符号结尾
EN

Stack Overflow用户
提问于 2022-05-26 16:23:26
回答 1查看 56关注 0票数 1

我的脚本从TFS读取一个路径,并向其中添加一个字符串,但在需要验证路径是否包含一个符号之前。

Example1:这就是路径,在本例中,我需要添加'/database/‘

代码语言:javascript
复制
$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22

Example2:我只需要添加“数据库/”

代码语言:javascript
复制
$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/

Example3:我需要加上/

代码语言:javascript
复制
$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database

我们的目标是继续使用path/database/所以我需要先检查路径,然后添加或删除‘数据库’字符串--任何人都可以帮助我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-26 18:59:47

如果我正确地理解了这个问题,您需要检查来自TFS的路径是否以正斜杠结尾,这样您就知道应该追加什么,因此您可以使用这样的小助手函数:

代码语言:javascript
复制
function Join-TFSPath {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $Path,
        [parameter(Mandatory = $false, Position = 1)]
        [string[]] $ChildPath,
        [char]$Separator = '/'
    )
    if ($ChildPath.Count) {
        "{0}$separator{1}$Separator" -f $Path.TrimEnd("\/"),
                                        (($ChildPath | ForEach-Object { $_.Trim("\/") } | 
                                          Where-Object { $_ -match '\S' }) -join $Separator)
    }
    else {
        "{0}$separator" -f $Path.TrimEnd("\/")
    }
}


# test if you need to add `database` or not

$tfsPath = '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database'
$folder  = 'database'

if (($tfsPath.TrimEnd("\/") -split '[\\/]')[-1] -ne $folder) {
    # use the function adding the $folder as ChildPath
    Join-TFSPath -Path $tfsPath -ChildPath $folder
}
else {
    # use the function without specifying the ChildPath so it will only ensure it 
    # ends with the chosen (or in this case default) separator character
    Join-TFSPath -Path $tfsPath
}

根据您的评论,您也许可以使用更专用的助手函数,例如:

代码语言:javascript
复制
function Append-TFSPath {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $Path,
        [parameter(Mandatory = $false, Position = 1)]
        [string] $ChildPath = 'database',
        [char]$Separator = '/'
    )
    $Path = $Path -replace '[\\/]+$'  # trim off final slash(es)
    $ChildPath = $ChildPath -replace '^[\\/]|[\\/]$' -replace '\\', $Separator
    if ([string]::IsNullOrWhiteSpace($ChildPath) -or ($Path -replace '\\', $Separator) -like "*$ChildPath") {
        "{0}$separator" -f $Path
    }
    else {
        "{0}$separator{1}$Separator" -f $Path, $ChildPath
    }
}

然后,只需按收到的方式发送路径到函数,它就会返回您想要的路径。

代码语言:javascript
复制
$tfsPath = '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database'
$folder  = 'V8.6.22/database'

Append-TFSPath -Path $tfsPath -ChildPath $folder
# because 'database' is the default value for the ChildPath parameter, you can leave that out:
# Append-TFSPath -Path $tfsPath

测试案例:

代码语言:javascript
复制
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22'
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/'
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database'
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database/'

都会返回$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72395012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档