"Jeff" <jeff.hillman@xxxxxx> wrote in message
news:1193213125.565389.64500@xxxxxx
> $number = 1
>
> Get-ChildItem AA_*.gif | Foreach-Object {
> Rename-Item $_.FullName `
> ( $_.Name -replace "\d{3}", ( "{0:000}" -f $number++ ) ) -
> WhatIf
> } With the approach above, the Foreach-Object isn't needed:
PS> $number = 1
PS> Get-ChildItem AA_*.gif | Rename-Item -newName {$_.Name -replace '\d{3}',
("{0:000}" -f $global:number++)} -whatif
However I would probably do it in a one-liner using foreach like so:
PS> Get-ChildItem AA_*.gif | Foreach {$num=1}{rename-item $_
($_.Name -replace '\d{3}', ("{0:000}" -f $num++)) -whatif}
Here Foreach is used to create sort of a dynamic cmdlet with a Begin and
Process scriptblock. Begin (the first scriptblock) initializes the $num
variable and Process (the second scriptblock) renames each file.
--
Keith