7

I'm looking to remove the final '/' character from a string that contains multiple storage paths in it, what is the best way to go about this, I keep getting close but I still can't quite get what I'm looking for, is a loop really the only way?

$Paths = /some/path/1/ /some/path/2/ /some/path/3/
$Paths = $Paths.Remove($Paths.Length - 1)
$Index = $Paths.LastIndexOf('/')
$ID = $Paths.Substring($Index + 1)

I'm currently getting errors like the following:

Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."

The desired final version of $Paths would be

/some/path/1 /some/path/2 /some/path/3

Any help would be greatly appreciated, I think I may have a process issue as well as a coding issue...

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
whoopn
  • 91
  • 1
  • 2
  • 7
  • Related post - [Powershell command to trim path if it ends with “\”](https://stackoverflow.com/q/37114587/465053) – RBT Apr 19 '18 at 12:00

3 Answers3

26

Use the .TrimEnd() method.

PS > $Paths = '/some/path/1/','/some/path/2/','/some/path/3/'
PS > $Paths
/some/path/1/
/some/path/2/
/some/path/3/
PS > $Paths = $Paths.TrimEnd('/')
PS > $Paths
/some/path/1
/some/path/2
/some/path/3
tommymaynard
  • 1,981
  • 12
  • 14
3

I was working on a function today where I needed to test the $Path switch to see if it ended with a '\' or not. This thread was helpful but I came up with another solution with a simple if statement.

The IF Statement tests the last character of the line by calculating the total length (minus 1 character) and if the last character is not equal to '\', the '\' gets added to the $Path value.

$Path = "C:\SomePath"

if ($Path.Chars($Path.Length - 1) -ne '\')
    {
        $Path = ($Path + '\')
    }

$Path Output = "C:\SomePath\"

To reverse it and remove the '\' is also a simple change using the TrimEnd() method.

$Path = "C:\SomePath\"

if ($Path.Chars($Path.Length - 1) -eq '\')
    {
        $Path = ($Path.TrimEnd('\'))
    }

$Path Output = "C:\SomePath"

  • The if statements can be simplified as follows: `if($path[-1] -ne '\') {$path += '\'}` or in reverse `if($path[-1] -eq '\') {$path.TrimEnd('\')}` In powershell, if you specify an index to a string object, it will index into the chars. Additionally, if you specify a negative index, it will read the chars from right to left. Here, an index of `-1` will give you the very last char in the string. – Devon Dieffenbach Nov 30 '22 at 16:49
0

other method, use foreach (or %) and remove last char with substring function:

$Paths = "/some/path/1/", "/some/path/2/", "/some/path/3/"
$Paths | %{$_.Substring(0, $_.length - 1) }
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • But what if you've got a path like `"some/path"` for some reason and then it cuts off the `"h"` and the path becomes incorrect? You should either check, if the last character is a slash, or use the solution of @tommymaynard – Impulse The Fox Apr 25 '17 at 09:02
  • 1
    Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 25 '17 at 12:18