Extract text between 2 underscores

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.

 

 

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…

Thank you.

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.

 

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.

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.

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]'

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?

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.