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)([^\.]+)(.+$)'