答案1
免责声明:此 PowerShell 代码尚未经过充分测试,无法确定它是否能够在所有环境中正确运行,以及是否具有各种可能奇怪的文件名/格式。但是,它确实适用于您提供的示例。使用风险自负或使用 Rename-Item
与 -WhatIf
-转变(因此它只会显示它会做什么而不会实际改变文件名)。
示例文件夹:
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
以下是完成任务的示例:
# 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
享受!