String Manipulation

Welcome Forums General PowerShell Q&A String Manipulation

Viewing 5 reply threads
  • Author
    Posts
    • #238949
      Participant
      Topics: 3
      Replies: 3
      Points: 33
      Rank: Member

      Hello

       

      I have variable $path. It contains more than 2 paths like:

      C:\user\mark\dekstop\file1.txt

      C:\user\tom\dekstop\file2.txt

      C:\user\mark\download\file3.txt

       

      I need to trim $path  (last *.txt)   to : (expectenced)

      C:\user\tom\dekstop\

      C:\user\mark\dekstop\

      Does anyone have ane idea how to trim it?

       

    • #238964
      Participant
      Topics: 5
      Replies: 2372
      Points: 6,005
      Helping Hand
      Rank: Community MVP

      Did you try to search for it? There’s even a builtin cmdlet for Split-Path !!!!!

    • #238970
      Participant
      Topics: 3
      Replies: 3
      Points: 33
      Rank: Member

      Yes I treid. I was focused to manipulation text with split, splitend and indexof.  i did’n knwo split-path cmdlet

    • #239303
      Participant
      Topics: 9
      Replies: 10
      Points: 47
      Rank: Member

      or you could use  both split and trim like:

      ($path.Split(';')).trim('*.txt')

    • #239312
      Participant
      Topics: 5
      Replies: 2372
      Points: 6,005
      Helping Hand
      Rank: Community MVP

      or you could use both split and trim like

      I’m afraid the .trim() method does not work like you think it does. Try the following snippet. It might help you to understand

      'trim specified charachters'.trim('srthcae')
    • #239327
      Participant
      Topics: 0
      Replies: 10
      Points: 108
      Rank: Participant

      Use the Split-Path command. Of course since your original variable is an array of paths you will need to iterate through that array and run the Split-Path on each element of the array.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.