Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

asked on

rename several files on a directory

Hello,

How can I rename several files on a directory and add _1 for distinct them :
$PrefixList = 'ZCAMER*', 'ZTOGO*'
Get-ChildItem -Path E:\Files -Include $PrefixList -Recurse |
      Group-Object -Property DirectoryName |
      ForEach-Object {
            $Directory = $_.Group
            $PrefixList | ForEach-Object {
                  $Prefix = $_
                  $Directory | Where-Object {$_.Name -like $Prefix} | Sort-Object -Property Name | Select-Object -First 1 | ForEach-Object {
                        $NewName = $_.BaseName.SubString(1) -replace '_\d{8}_\d{6}\Z'
                        Rename-Item -Path $_.FullName -NewName $NewName -WhatIf
                        $_ | Select-Object 'DirectoryName', 'Name', @{n='NewName'; e={$NewName}}
                  }
            }
      }

Thanks

Regards
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bibi92

ASKER

Thanks regards