0

Batch delete duplicate words in file name the code is posted in the this post, but it does not work with me Deleting repeated parts of a file name?

Get-ChildItem -Path .\* -File -Recurse | ForEach-Object {
    Rename-Item $_ -NewName ((($_.Basename.Split("_") | Select-Object -Unique) -Join "_") + $($_.Extension))
}
  • What exactly does not work? Do you get an error message? This code splits on _ is this good for your filenames? if you don't have an underscore anywhere, it won't do anything – SimonS Feb 25 '23 at 10:25
  • 1
    Try: (Get-ChildItem -Path .\* -File -Recurse) | Rename-Item -NewName { (@($_.BaseName.Split('_') | select -unique) -join '_') + $_.Extension } -WhatIf. – Keith Miller Feb 25 '23 at 10:50
  • does not work for files in sub folders and this appear: Rename-Item : Cannot rename because item at 'A1 A1.docx' does not exist. At line:2 char:5
    • Rename-Item $_ -NewName ((($_.Basename.Split(" ").ToUpper() | Sel ...
      
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      • CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
      • FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
    – m26348762 Feb 25 '23 at 21:38
  • 1
    That is not the code @KeithMiller gave you.. – Theo Feb 26 '23 at 12:11
  • @Theo: Thank you! m26348762: EDIT YOUR QUIESTION! Add sample filenames, formatted CORRECTED code, and error messages to your edited question... – Keith Miller Feb 26 '23 at 16:22

0 Answers0