I have a question regarding changing names of multiple files (approximately 800) to fit specific name format.
Exemplary data:
- 123_12345_abc455.jpg
- 123_12345_abc677.jpg
- 123_12345_abc899.jpg
- 123_47847_qwe334.jpg
- 123_47847_qwe433.jpg
- 123_54321_uwu123.jpg
Objective:
- 12345_1.jpg
- 12345_2.jpg
- 12345_3.jpg
- 47847_1.jpg
- 47847_2.jpg
- 54321_1.jpg
Note that after we remove characters from start and end we are left with multiple files of the same name, which causes errors and script simply omits those files with same name.
What I've got so far is:
ls | Rename-Item -NewName {$_.name.substring(0,$_.BaseName.length-7) $_.Extension}
CodePudding user response:
Here's one technique:
Get-ChildItem -Path .\*.jpg |
Group-Object {$_.Name.Split('_')[1]} |
ForEach-Object {
$namePart = $_.Name
$_.Group |
ForEach-Object {$i=1}{
Rename-Item -Path $_.FullName -NewName ("$namePart`_" $i $_.Extension)
}
}