Removing the SPACE before the second occurring of underscore "_"

Hello There:

Greetings all!

Requesting you to help with this specific request:

Would like to remove the space just before the second occurrence of “_” and rename the file using the PowerShell

For Example: 221231_John Reeves R _JoiningLetter.pdf to 221231_John Reeves R_JoiningLetter.pdf

and using this cmdlet to list those pdf’s which are having space just before the second instance of “_”

gci . -recurse -filter *.pdf| where {$_.basename -match "(^(\d{8})(_)([a-zA-Z ]*)( )(_))" }

and please note, we cant suppress all spaces in the filename as the second string which contains employee name will contain whitespaces.

Appreciated!

Cheers,

 

 

 

 

 

 

 

 

 

No big deal … simply replace all white spaces followed by an underline …

‘221231_John Reeves R JoiningLetter.pdf’ -replace '\s+(?=)’

Olaf: thanks for the response…

 

However as I noted, we can’t suppress all spaces but only one before the second occurrence of the “_” …

Are there more than one space before an underline charachter? … did you try the code snippet? … it removes ONLY the space befor the underline. The other spaces remain in the string.

Should work as Olaf says.
But if you have more than one space or need to match an exact number of spaces before the underscore you can add a quantifier.
E.g. ‘\s{1}(?=_)’

But that seem to be pretty strange filename requirements if you ask me :slight_smile:

A tip is to use a regex tester, there are several online that simplifies regex testing.
E.g.
https://regex101.com/

Actually I meant “Are there more than one occurence of a space in front of an underline charachter?” :wink:

I strongly support Fredrik’s suggestion of Regex101. It was a lifesaver for developing Luhn Algorithms.

Throw the regex in, then throw in your sample filename, and play with it until you match what you need.

I also strongly suggest checking out https://www.rexegg.com/ for detailed explanations of some of the concepts in RegEx.

Olaf - no chance of double space there as I have another cmdlet which suppresses the double whitespace at a position.

Fredrik - yes its the archived data for the last decade and am assisting to set things right …phase by phase

and i have used “https://rubular.com/” for coming to a conclusion of finding those files - using regex, now got stuck with the rename options within powershell

Cheers…

 

 

thanks Charles for the URL… Noted

Very similar to the above requested, I’ve resolved the space and renamed the file where it was after the first occurrence of Underscore “_”

For Example:

12345678_ John Reeves R_JoiningLetter.PDF

will be renamed as

12345678_John Reeves R_JoiningLetter.PDF

using::

gci . -recurse -filter *.pdf | where {$_.basename -match "(^(\d{8})(_)(( )[a-zA-Z ]*))"} | rename-item -newname { $_.Name -replace '(.{9}).(.+)','$1$2' }
In these cases the position is fixed that is 10th position of the string and so removing space then renaming the file is pretty straightforward.

So the assistance required here is for the space before the second instance of underscore “_”

This is the logic I came up with for the resolving the cases of have the space just before the “Underscore” , if any better logic can be implemented, am open for them…

Kindly advise!

Cheers,

 

 

I really do not understand what’s wrong with my suggestions. The code snippet I posted matches ONLY spaces occuring in front of an underline - NO OTHER OCCURENCES OF ANY WHITE SPACE CHARACHTERS. :wink:

Did you try it at least?

Olaf ---- Yes yes its worked , sorry i missed out the snippet early…

 

Worked brilliantly …

Cheers …

 

Thank you. Now I can sleep peacefully again. :wink: :smiley: