0

i have network path and i need to get the filename to a variable for example:

$path1 = '\\192.168.10.10\bla\bla\bla\120\s$\filename.exe'
$path2 = '\\srv\c$\bla\bla\120\s$\file2.exe'

i would then want to have

$var1 = filename.exe
$var2 = filename.exe

I'm not quite sure how it can be done with regex, I want to strip everything but the last '' character and what comes after it Can anyone assist please?

-- EDIT Ok, I have found a way but it looks a bit stupid:

$temp = $path1.split('\')
$var1 = $temp[$temp.lenth -1]

there must be more elegant way for this!

Paolo
  • 21,270
  • 6
  • 38
  • 69
Shahar Weiss
  • 141
  • 1
  • 2
  • 11

1 Answers1

1

No regex required. You can use GetFileName:

$var1 = [System.IO.Path]::GetFileName($path1)
$var2 = [System.IO.Path]::GetFileName($path2)
Paolo
  • 21,270
  • 6
  • 38
  • 69