Extract text between 2 underscores

Welcome Forums General PowerShell Q&A Extract text between 2 underscores

  • This topic has 7 replies, 4 voices, and was last updated 3 weeks ago by
    Participant
    .
Viewing 7 reply threads
  • Author
    Posts
    • #201843
      Participant
      Topics: 2
      Replies: 3
      Points: -1
      Rank: Member

      I need to rename a large number of PDF files that are in the following format:

      _TEXT[1,2…n]_NAME[1,2,…n]_SUFFIX[1,2…n]

      Is there a way to extract TEXT [1…n] (amount of characters is different for every file) between two underscores using Power-shell?

      Is there a way to extract SUFFIX [1,2…n] (amount of characters is different for every file)?

      The above two questions are two separate instances.

       

       

    • #201855
      Moderator
      Topics: 3
      Replies: 200
      Points: 1,052
      Helping Hand
      Rank: Community Hero

      Hi,

      Yes, it is possible. Did you try anything, if so please paste the code here, we will guide you in the right direction.

      Please refer to the complete string operations in the link below…

      String Operations in PowerShell

      Thank you.

    • #201867
      Participant
      Topics: 2
      Replies: 3
      Points: -1
      Rank: Member

      Thank you, Kiran. I used Dir | Rename-Item {$_.name -replace …} to get rid of / replace similar characters. I don’t know how to delete text between two characters (underscores in our case). And another question is how to delete something after an underscore at the end of a string of characters.

       

    • #201876
      Participant
      Topics: 2
      Replies: 3
      Points: -1
      Rank: Member

      Looks like I need to first use

      $Files.BaseName | Select-String -Pattern ‘_([^_]+)’ -AllMatches | Foreach-Object {

      but I don’t know how to incorporate Remove-Item or something similar to get rid of characters between two underscores.

    • #201885
      Participant
      Topics: 2
      Replies: 1780
      Points: 3,735
      Helping Hand
      Rank: Community Hero

      Haven’t you been satisfied with the answers you’ve got at SO? https://stackoverflow.com/questions/60052406/extract-text-between-two-underscores

      When you crosspost the same question at the same time to different forums you should at least post links to the crossposts in each forum to avoid other people making work twice or even more.

      • This reply was modified 3 weeks ago by Olaf.
    • #201903
      Participant
      Topics: 9
      Replies: 436
      Points: 762
      Helping Hand
      Rank: Major Contributor

      Example code:

      $Sample = '_TEXT[1,2...n]_NAME[1,2,...n]_SUFFIX[1,2...n]'
      
      $First  = $Sample.Split('_')[1]
      $Second = $Sample.Split('_')[2]
      $Third  = $Sample.Split('_')[3]
      
      "First  is '$First'"
      "Second is '$Second'"
      "Third  is '$Third'"
      

      Output:

      First  is 'TEXT[1,2...n]'
      Second is 'NAME[1,2,...n]'
      Third  is 'SUFFIX[1,2...n]'
      
    • #202130
      Participant
      Topics: 2
      Replies: 3
      Points: -1
      Rank: Member

      Olaf, thank you for the input!!!

      My post here is an extension of posts from other forums. I am still looking for a solution. Are you able to help?

      • This reply was modified 3 weeks ago by AA_Meow01.
    • #202139
      Participant
      Topics: 2
      Replies: 1780
      Points: 3,735
      Helping Hand
      Rank: Community Hero

      Probably I would but as stated alreaydy in SO you did not explain what exactly you need to do. You should post some example file names and some examples of the expected result to clarify what you want to achieve.

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