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:
- AB_NAME_NAME_NAME_NAME_DS_123_EN_6.pdf
- AC_NAME_NAME_NAME_DS_321_EN_10.pdf
- AD_NAME_NAME_DS_321_EN_101.pdf
And would like the bold part to be removed to become:
- AB_NAME_NAME_NAME_NAME_DS_123_EN.pdf
- AC_NAME_NAME_NAME_DS_321_EN.pdf
- AD_NAME_NAME_DS_321_EN.pdf
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.