Windows Vista Forums

Renumbering Files In Powershell?

  1. #1


    Barry S. Guest

    Renumbering Files In Powershell?

    I have a list of files in a directory.



    AA_001_ABC.gif
    AA_005_QRZ.gif
    AA_302_LMU.gif

    I want to renumber the files sequentially without modifying any other
    part of the filename.

    In Powershell, how would I search within the filename for a 3 digit #
    (regex) and then replace just the number within the filename with a
    value from an iteration (I=1, ++i)

    End Result:

    AA_001_ABC.gif
    AA_002_QRZ.gif
    AA_003_LMU.gif

    Thanks.


      My System SpecsSystem Spec

  2. #2


    Jeff Guest

    Re: Renumbering Files In Powershell?

    On Oct 24, 2:20 pm, Barry S. <b...@xxxxxx> wrote:

    > I have a list of files in a directory.
    >
    > AA_001_ABC.gif
    > AA_005_QRZ.gif
    > AA_302_LMU.gif
    >
    > I want to renumber the files sequentially without modifying any other
    > part of the filename.
    >
    > In Powershell, how would I search within the filename for a 3 digit #
    > (regex) and then replace just the number within the filename with a
    > value from an iteration (I=1, ++i)
    >
    > End Result:
    >
    > AA_001_ABC.gif
    > AA_002_QRZ.gif
    > AA_003_LMU.gif
    >
    > Thanks.
    The following will rename all the files starting with "AA_" and ending
    with ".gif":

    $number = 1

    Get-ChildItem AA_*.gif | Foreach-Object {
    Rename-Item $_.FullName `
    ( $_.Name -replace "\d{3}", ( "{0:000}" -f $number++ ) ) -
    WhatIf
    }

    I put the -WhatIf switch on the end of the Rename-Item call so you can
    make sure this will do what you want it to do.

    Jeff


      My System SpecsSystem Spec

  3. #3


    Keith Hill [MVP] Guest

    Re: Renumbering Files In Powershell?

    "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


      My System SpecsSystem Spec

  4. #4


    Charlie Russel - MVP Guest

    Re: Renumbering Files In Powershell?

    I learn something every day I read here. Thanks!

    --
    Charlie.
    http://msmvps.com/xperts64
    http://mvp.support.microsoft.com/profile/charlie.russel


    "Keith Hill [MVP]" <r_keith_hill@xxxxxx_no_spam_I> wrote in message
    news:A7452729-DFC0-432D-85B6-D4FCBF2EFD4C@xxxxxx

    > "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

      My System SpecsSystem Spec

Renumbering Files In Powershell?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Powershell and ini files Selko PowerShell 22 07 Jun 2008
Files created by Powershell are over 2X bigger than files created bytext editor or cmd.exe. tom.luxury PowerShell 4 27 May 2008
Rename files with powershell Michael Julson PowerShell 15 20 Mar 2008
How does PowerShell use the help.xml files? Andrew Watt [MVP] PowerShell 1 15 Nov 2006
A way to zip/unzip zip files in PowerShell? Jon Miller PowerShell 8 14 Jul 2006