rename - Renaming a file name to exclude the first couple parts Powershell -


i have on million files such: first_last_mi_dob_ , lots more information. there way can run rename script can remove first, last, mi, , dob file name, keep stuff after that? thank you.

edited answer question: parse , switch elements of folder names using powershell

# path folder $path =  '.\'  # regex match "id_000000..." $regex = 'id_\d+.*$'  # objects in path get-childitem -path $path |     # select objects not directory , name matches regex     where-object {!$_.psiscontainer -and $_.name -match $regex} |         # each such object         foreach-object {             # rename object             rename-item -path $_.fullname -newname $matches[0]         } 

update #1 : seems need write regex match required part of name , use in rename document.

assuming file name x-john_doe_._dob_01-11-1990_m_id_000000_titleofdocument_dateofdocument_docpagenu‌​mber_, here couple of examples:


  • regex (https://regex101.com/r/gi0fz2/2): (id_\d+.*)$ - match id_{one_or_more_digits}{any_characters}
  • result:id_000000_titleofdocument_dateofdocument_docpagenu‌​mber_

  • regex (https://regex101.com/r/gi0fz2/1): \d{4}_(m|f)_(.*)$ - match {4_digits}_m_{or}_f_ , capture after in capture group.
  • result:
    • 1st match - m
    • 2nd match (the 1 use) - id_000000_titleofdocument_dateofdocument_docpagenu‌​mber_

update #2:

all names in each file different, long different id's. example: john_doe_dob_01/01/01_id_000000 , next file name be: john_smith_dob_01/02/01_id_100000 , on. thinking want read file name in string, split _ , make new file name stuff [4] , after. there way that?

sure, can that, i'd recommend regex approach, because work every filename has id_0xxxx string, no matter of what. i've modified initial example first regex, should work you.

but if you'd try splitting approach, here how it:

# path folder $path =  '.\'  # filename separator $separator = '_'  # objects in path get-childitem -path $path |     # select objects not directory , name matches regex     where-object {!$_.psiscontainer} |         # each such object         foreach-object {             # generate new name             $newname = ($_.name -split $separator | select-object -skip 4) -join $separator              # rename object             rename-item -path $_.fullname -newname  $newname         } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -