• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

Powershell file directoy question

I have a file in the following location

c:\data\users\sales\reps\

Please could somebody help me with a  powershell script that will return only the following:

  \sales\reps\

and if possible

c:\..\sales\reps\

Many thanks
0
Total_Nutter
Asked:
Total_Nutter
1 Solution
 
David Johnson, CD, MVPOwnerCommented:
not elegant but it works
$a = "c:\data\users\sales\reps\"
$b = Split-Path $a -Leaf
$c = Split-Path $a -Parent
$d = split-path $c -leaf
$e = "\" + $d + "\" + $b + "\"
$f = "c:\.."+$e
$e
$f
0
 
Brent ChallisPrincipal: ITCommented:
I like to write my functionality as a function so that I cane make it more flexible, or if I think it would be of general use, I can add it to my profile.

Does this achieve what you require?

function Get-PartPath
{
    Param
    (
        [String]$FullPath,
        [string]$MaskPath,
        [string]$Prefix = ""
    )
    return ($Prefix +  $fullPath.Substring($maskPath.Length))
$path
}

Get-PartPath -FullPath "c:\data\users\sales\reps\" -MaskPath  "c:\data\users" -Prefix "c:\.."

If you always use the same value for the mask you could make it a default value:
 [string]$MaskPath =  "c:\data\users",
0
 
Total_NutterAuthor Commented:
Simple, but works - many thanks
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now