Windows Vista Forums

Regex rename file with date
  1. #1


    Personne Guest

    Regex rename file with date

    I have a bunch of file with this format YYYY - name.ext
    1925 - test1.txt
    1944 - test2.txt
    2008 - test3.txt



    and I would like to rename these files to
    test1 - 1925.txt
    test2 - 1944.txt
    test3 - 2008.txt

    I'm trying this with no success
    dir | rename-item -newname {$_.name -replace '^(\d\d\d\d) -
    (.*)',"$matches[2] - $matches[1]"} -whatif

    Thanks for your help

      My System SpecsSystem Spec

  2. #2


    tojo2000 Guest

    Re: Regex rename file with date

    On Aug 30, 9:18*am, Personne <cpdiv...@xxxxxx> wrote:

    > I have a bunch of file with this format YYYY - name.ext
    > 1925 - test1.txt
    > 1944 - test2.txt
    > 2008 - test3.txt
    >
    > and I would like to rename these files to
    > test1 - 1925.txt
    > test2 - 1944.txt
    > test3 - 2008.txt
    >
    > I'm trying this with no success
    > dir | rename-item -newname {$_.name -replace '^(\d\d\d\d) -
    > (.*)',"$matches[2] - $matches[1]"} -whatif
    >
    > Thanks for your help
    How about something like this?

    dir |
    %{$parts = [regex]::split($_.basename, ' - ')
    Rename-item $_ ("{0} - {1}{2}" -f $parts[1], $parts[0],
    $_.Extension)
    }

      My System SpecsSystem Spec

  3. #3


    Shay Levy [MVP] Guest

    Re: Regex rename file with date

    dir | rename-item -NewName {$_.name -replace '^(\d+)(\s\-\s)([^\.]+)(.+$)','$3$2$1$4'}


    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar: http://tinyurl.com/PSToolbar



    P> I have a bunch of file with this format YYYY - name.ext
    P> 1925 - test1.txt
    P> 1944 - test2.txt
    P> 2008 - test3.txt
    P> and I would like to rename these files to
    P> test1 - 1925.txt
    P> test2 - 1944.txt
    P> test3 - 2008.txt
    P> I'm trying this with no success
    P> dir | rename-item -newname {$_.name -replace '^(\d\d\d\d) -
    P> (.*)',"$matches[2] - $matches[1]"} -whatif
    P> Thanks for your help
    P>



      My System SpecsSystem Spec

  4. #4


    Personne Guest

    Re: Regex rename file with date

    Thanks Shay

    Anyway $4 is not necessary

    dir | rename-item -NewName {$_.name -replace '^(\d+)(\s\-\s)([^\.]+)(.
    +$)','$3$2$1'}



    On Aug 30, 2:15*pm, Shay Levy [MVP] <n...@xxxxxx> wrote:

    > dir *| rename-item -NewName {$_.name -replace '^(\d+)(\s\-\s)([^\.]+)(.+$)','$3$2$1$4'}
    >
    > ---
    > Shay Levy
    > Windows PowerShell MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic
    > PowerShell Toolbar:http://tinyurl.com/PSToolbar
    >
    > P> I have a bunch of file with this format YYYY - name.ext
    > P> 1925 - test1.txt
    > P> 1944 - test2.txt
    > P> 2008 - test3.txt
    > P> and I would like to rename these files to
    > P> test1 - 1925.txt
    > P> test2 - 1944.txt
    > P> test3 - 2008.txt
    > P> I'm trying this with no success
    > P> dir | rename-item -newname {$_.name -replace '^(\d\d\d\d) -
    > P> (.*)',"$matches[2] - $matches[1]"} -whatif
    > P> Thanks for your help
    > P>

      My System SpecsSystem Spec

  5. #5


    tojo2000 Guest

    Re: Regex rename file with date

    On Aug 31, 3:26*pm, Personne <cpdiv...@xxxxxx> wrote:

    > Thanks Shay
    >
    > Anyway $4 is not necessary
    >
    > dir *| rename-item -NewName {$_.name -replace '^(\d+)(\s\-\s)([^\.]+)(.
    > +$)','$3$2$1'}
    >
    > On Aug 30, 2:15*pm, Shay Levy [MVP] <n...@xxxxxx> wrote:
    >
    >
    >

    > > dir *| rename-item -NewName {$_.name -replace '^(\d+)(\s\-\s)([^\.]+)(.+$)','$3$2$1$4'}
    >

    > > ---
    > > Shay Levy
    > > Windows PowerShell MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic
    > > PowerShell Toolbar:http://tinyurl.com/PSToolbar
    >

    > > P> I have a bunch of file with this format YYYY - name.ext
    > > P> 1925 - test1.txt
    > > P> 1944 - test2.txt
    > > P> 2008 - test3.txt
    > > P> and I would like to rename these files to
    > > P> test1 - 1925.txt
    > > P> test2 - 1944.txt
    > > P> test3 - 2008.txt
    > > P> I'm trying this with no success
    > > P> dir | rename-item -newname {$_.name -replace '^(\d\d\d\d) -
    > > P> (.*)',"$matches[2] - $matches[1]"} -whatif
    > > P> Thanks for your help
    > > P>
    If you want to nitpick, you don't need to escape the dash outside of a
    character class, you can just use a space to indicate a space unless
    you think it might be some other whitespace character for some reason,
    and you don't really need the $ anchor at the end since .+ will eat up
    anything until the end of the string. Not to say that it's the
    "right" way, but just another way.

    '^(\d+)( - )([^\.]+)(.+)'
    vs.
    '^(\d+)(\s\-\s)([^\.]+)(.+$)'

      My System SpecsSystem Spec

Regex rename file with date problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Rename file with current date/time Vadims Podans [MVP] PowerShell 0 10 Sep 2009
Rename errorin Vista - multiple file rename James136 Vista file management 3 04 Apr 2008
Photo date taken vs. file date theFinn Vista music pictures video 0 06 Feb 2008
Regex and File Spliting Question Rob Kenny PowerShell 4 05 Apr 2007