Disclaimer: This PowerShell-code has not been sufficiently tested to know that it will work properly in all environments with all kinds of possibly strange filenames / formats. But, it does work on your provided examples. Use at your own risk or use Rename-Item with the -WhatIf-switch (so it will only show what it would do without actually atering the filename).
Sample folder:
CDS 202 - GLAZING PACKERS_CDS 202 - Glazing Packers_CDS 202 - Glazing Packers.docx
CDS 202 - GLAZING PACKERS_CDS 202 - Glazing Packers_CDS 202 - Glazing Packers.pdf
CDS 202 - GLAZING PACKERS_PX-INA-PD-RP-X-XX-XX-0026.pdf
Here are examples how to accomplish the task:
# Remove all duplicates in filenames in current folder: (Case Sensitive)
Get-ChildItem -Path .\* -File | ForEach-Object {
Rename-Item $_ -NewName ((($_.Basename.Split("_") | Select-Object -Unique) -Join "_") + $($_.Extension))
}
# Results:
# CDS 202 - GLAZING PACKERS_CDS 202 - Glazing Packers.docx
# CDS 202 - GLAZING PACKERS_CDS 202 - Glazing Packers.pdf
# CDS 202 - GLAZING PACKERS_PX-INA-PD-RP-X-XX-XX-0026.pdf
# Remove all duplicates in filenames in current folder: (Case Insensitive - Drawback: filenames are converted to upper case)
Get-ChildItem -Path .\* -File | ForEach-Object {
Rename-Item $_ -NewName ((($_.Basename.Split("_").ToUpper() | Select-Object -Unique) -Join "_") + $($_.Extension))
}
# Results:
# CDS 202 - GLAZING PACKERS.docx
# CDS 202 - GLAZING PACKERS.pdf
# CDS 202 - GLAZING PACKERS_PX-INA-PD-RP-X-XX-XX-0026.pdf
# Remove all duplicates in filenames in current folder and all subfolders: (Case Sensitive)
Get-ChildItem -Path .\* -File -Recurse | ForEach-Object {
Rename-Item $_ -NewName ((($_.Basename.Split("_") | Select-Object -Unique) -Join "_") + $($_.Extension))
}
# Results:
# CDS 202 - GLAZING PACKERS_CDS 202 - Glazing Packers.docx
# CDS 202 - GLAZING PACKERS_CDS 202 - Glazing Packers.pdf
# CDS 202 - GLAZING PACKERS_PX-INA-PD-RP-X-XX-XX-0026.pdf
# Remove all duplicates in filenames in current folder and all subfolders: (Case Insensitive - Drawback: all filenames are converted to upper case)
Get-ChildItem -Path .\* -File -Recurse | ForEach-Object {
Rename-Item $_ -NewName ((($_.Basename.Split("_").ToUpper() | Select-Object -Unique) -Join "_") + $($_.Extension))
}
# Results:
# CDS 202 - GLAZING PACKERS.docx
# CDS 202 - GLAZING PACKERS.pdf
# CDS 202 - GLAZING PACKERS_PX-INA-PD-RP-X-XX-XX-0026.pdf
Enjoy!
this_is_a_long_name_which_is_an_example.ext- if you "deduplicate" the name, it would most likely becomethis_is_a_long_name_whichn_example.ext, as_is_ais mentioned twice. so if you don't have an exact schema that all new file names share (e.g.file_name_-_file-name - file name.ext), it is nearly impossible to do that in an automated way. – flolilo Oct 30 '17 at 13:31_character. So powershell could easily split the filename at_and then remove any duplicates in the resulting array. Finally, it could use the cleaned up array to build a new filename without duplicates. – Appleoddity Oct 30 '17 at 13:44.Split()theBaseName,.Replace()the word-separator (space, dot, underscore, hyphen,...) so they are all the same, thenSort-Object -Uniqueto compare them, then.Join()the substrings back again and use them as newBaseNameinRename-Item. – flolilo Oct 30 '17 at 13:50subst j: .A J folder is now added in explorer which links to the current path. From there you can do anything you want. – LPChip Oct 30 '17 at 14:05