crazymatt
crazymatt

Reputation: 3286

Powershell Remove Text Between before the file extension and an underscore

I have a few hundered PDF files that have text in their file names which need to be removed. Each of the file names have several underscores in their names depending on how long the file name is. My goal is to remove the text in that exists between the .pdf file extension and the last _.

For example I have:

And would like the bold part to be removed to become:

I am a novice at powershell but I have done some research and have found Powershell - Rename filename by removing the last few characters question helpful but it doesnt get me exactly what I need because I cannot hardcode the length of characters to be removed because they may different lengths (2-4)

Get-ChildItem 'C:\Path\here' -filter *.pdf | rename-item -NewName {$_.name.substring(0,$_.BaseName.length-3) + $_.Extension}

It seems like there may be a way to do this using .split or regex but I was not able to find a solution. Thanks.

Upvotes: 2

Views: 2207

Answers (3)

user6811411
user6811411

Reputation:

Just to show another alternative,

  • the part to remove from the Name is the last element from the BaseName splitted with _
  • which is a negative index from the split [-1]
    Get-ChildItem 'C:\Path\here' -Filter *.pdf |%{$_.BaseName.split('_\d+')[-1]}
    6
    10
    101
  • as the split removes the _ it has to be applied again to remove it.

Get-ChildItem 'C:\Path\here' -Filter *.pdf | 
   Rename-Item -NewName { $_.Name -replace '_'+$_.BaseName.split('_')[-1] } -whatif

EDIT a modified variant which splits the BaseName at the underscore
without removing the splitting character is using the -split operator and
a RegEx with a zero length lookahead

> Get-ChildItem 'C:\Path\here' -Filter *.pdf |%{($_.BaseName -split'(?=_\d+)')[-1]}
_6
_10
_101

Get-ChildItem 'C:\Path\here' -Filter *.pdf | 
    Rename-Item -NewName { $_.Name -replace ($_.BaseName -split'(?=_)')[-1] } -whatif

Upvotes: 0

mklement0
mklement0

Reputation: 439872

Using the -replace operator with a regex enables a concise solution:

Get-ChildItem 'C:\Path\here' -Filter *.pdf | 
  Rename-Item -NewName { $_.Name -replace '_[^_]+(?=\.)' } -WhatIf

-WhatIf previews the renaming operation. Remove it to perform actual renaming.

  • _[^_]+ matches a _ character followed by one or more non-_ characters ([^-])

    • If you wanted to match more specifically by (decimal) digits only (\d), use _\d+ instead.
  • (?=\.) is a look-ahead assertion ((?=...)) that matches a literal . (\.), i.e., the start of the filename extension without including it in the match.

  • By not providing a replacement operand to -replace, it is implicitly the empty string that replaces what was matched, which effectively removes the last _-prefixed token before the filename extension.


You can make the regex more robust by also handling file names with "double" extensions; e.g., the above solution would replace filename a_bc.d_ef.pdf with a.c.pdf, i.e., perform two replacements. To prevent that, use the following regex instead:

$_.Name -replace '_[^_]+(?=\.[^.]+$)'

The look-ahead assertion now ensures that only the last extension matches: a literal . (\.) followed by one or more (+) characters other than literal . ([^.], a negated character set ([^...])) at the end of the string ($).

Upvotes: 1

TheMadTechnician
TheMadTechnician

Reputation: 36332

You can use the LastIndexOf() method of the [string] class to get the index of the last instance of a character. In your case this should do it:

Get-ChildItem 'C:\Path\here' -filter *.pdf | rename-item -NewName { $_.BaseName.substring(0,$_.BaseName.lastindexof('_')) + $_.Extension }

Upvotes: 3

Related Questions