Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Files To Fit Standard

In a personnel folder there are files regarding coaching for employees that were created by the persons supervisor. The naming convention for these files ALWAYS should be like

Supervisorname.SXXEXX.NameOfIssue.doc

So a breakdown further:

  • Supervisorname = Bob Godfrey
  • SXX = stipulation number (always 2 digits ranges from 01 - 99, but not all numbers are used)
  • EXX = employee number (always 2 digits, we have not hit the point where we need to move to 3 digits or to a diff prefix other than E yet....)

So a sample name would be Bob Godfrey.S48E99.Issue With Blue.doc - however, since some supervisors did not follow the rules there are files without the . seperator, we have files that look like

Bob Godfrey - S48E99 - Issue With Blue.doc
Bob Godfrey X S48E99 - Issue With Blue.doc
Bob Godfrey S48E99 - Issue With Blue.doc

For normalization purposes, (and for me not to pull my hair out in the process) I tried to use the below syntax to grab these outliers and rename them (there are roughly 300) now while this seems to execute as it should, the files are not renamed.

What do I need to change to get these files renamed accordingly?

Get-ChildItem -Path R:\Personnel\ -Filter *.doc |
    Rename-Item -NewName { $_.BaseName.Replace(" - ", ".") + $_.Extension } -Verbose

Get-ChildItem -Path R:\Personnel\ -Filter *.doc |
    Rename-Item -NewName { $_.BaseName.Replace(" X ", ".") + $_.Extension } -Verbose
like image 858
BellHopByDayAmetuerCoderByNigh Avatar asked Dec 20 '25 11:12

BellHopByDayAmetuerCoderByNigh


1 Answers

Use a regular expression replacement to center your matches around the stipulation/employee number part of the filename:

Rename-Item -NewName { $_.Name -replace ' (?:[-X] )?(S\d\dE\d\d)(?: [-X])? ', '.$1.' }
like image 99
Ansgar Wiechers Avatar answered Dec 22 '25 23:12

Ansgar Wiechers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!