Home > Software engineering >  Powershell - Need to check if name is ended with a sign
Powershell - Need to check if name is ended with a sign

Time:05-29

My script read a path from TFS and I add a string to it but before I need to verify that the path contains or not contains a sign

Example1: This is the path, in this case I need to add '/database/'

$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22

Example2: I need to add only 'database/'

$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/

Example3: I need to add '/'

$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database

The goal is to continue with the script with the path/database/ so I need to check first the path and then to add or remove the 'database' string Anyone can help me with that please?

CodePudding user response:

If I understand the question properly, you want to check if the path from TFS ends with a forward slash or not, so you would know what to append to it and for thast you could use a small helper function like this:

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
}

As per your comment, you could perhaps then use a more dedicated helper function like:

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
    }
}

Then, just send the path as you have received it to the function and it will return the path you want

$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

Testcases:

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/'

will all return $/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database/

  • Related