Find the third indexOf a character in string

I have a string where in i'm trying to capture the substring before the third occurence of a slash (\) character.

I have used IndexOf. Is there a neater way to capture this.

$mainstring = "C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx"
$indexof1 = $mainstring.IndexOf("\")
$indexof2 = $mainstring.IndexOf("\",$indexof1+1)
$indexof3 = $mainstring.IndexOf("\",$indexof2+1)
$subsetstring = $mainstring.Substring(0, $indexof3)
Write-Host $subsetstring


July 27th, 2015 11:17am

'C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx' -split '\\'

OR

PS > Split-Path 'C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx'
C:\ClusterStorage\Volume1\CSV-VM
PS > Split-Path 'C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx'  -leaf
CSV-VM-VHD.vhdx
PS >

 

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 11:41am

Hi,

You could also do something like this:

$str = 'C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx'
$filePath = "{0}\{1}\{2}" -f $str.Split('\')

$filePath

July 27th, 2015 11:54am

Ok - try this:

PS > $s='C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx'
PS > $s.Substring(0,$s.LastIndexOf('\'))

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 11:58am

jrv - I believe that the OP is looking to get the output one level higher.

C:\ClusterStorage\Volume1 vs C:\ClusterStorage\Volume1\CSV-VM

That's what the original code spits out at least.

July 27th, 2015 12:00pm

Another is:

$s.Remove($s.LastIndexOf('\'))

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 12:00pm

I'm trying to capture before the third occurence. In this case 'C:\ClusterStorage\Volume1'
July 27th, 2015 12:01pm

I'm trying to capture before the third occurence. In this case 'C:\ClusterStorage\Volume1'

See my post above, that'll get you what you're after.

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 12:02pm

I would use either split or Split-Path

Split-Path (Split-Path $s)

July 27th, 2015 12:08pm

There is also another construct which can be useful;

([io.directoryinfo]$s).Parent.Parent.Fullname

An similarly with FileInfoo.

 ([io.fileinfo]$s).Directory.Parent.Fullname

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 12:11pm

THe simplest and eeasiest to remember is this:

Split-Path (Split-Path $s)

Just do a walkback foor ass many parents as needed.

Split-Path (Split-Path (Split-Path $s))

This will walk-back one more folder.

Mike's method is also very easy to use.

July 27th, 2015 12:16pm

$mainstring   = "C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx"
$s = $mainstring.split('\')
$subsetstring = $($s[0],$s[1],$s[2]) -join '\'

Write-Host $subsetstring

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 5:27pm

Oh!  I see!

Like this:

$s='C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx'.Split('\')
[string]::Join('\',$s[0..2])

July 27th, 2015 6:22pm

The equivalent is:

Get-Aduser -Filter '-not (employeeType -eq "terminated")'

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 6:47pm

$s='C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx'
[string]::Join($s[2],$s.Split($($s[2]))[0..2])


July 27th, 2015 9:11pm

capture the substring before the third occurrence of a slash (\) character

$s = 'C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx'
$d = '\'
$n = 3
[string]::Join($d,$s.Split($d)[0..$($n-1)])




Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 1:09am

capture the substring before the third occurrence of a slash (\) character

$s = 'C:\ClusterStorage\Volume1\CSV-VM\CSV-VM-VHD.vhdx'
$d = '\'
$n = 3
[string]::Join($d,$s.Split($d)[0..$($n-1)])




  • Edited by LarryWeiss Tuesday, July 28, 2015 12:32 PM
July 28th, 2015 1:09am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics