Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Batch rename of files

38 views
Skip to first unread message

robm_jnb

unread,
Jun 13, 2008, 3:22:00 AM6/13/08
to
Hi everyone,

I have a problem in that I need to compose a PowerShell script to rename
about 200 files. These files are the in the example format:

sample pdf document.pdf_(2008-05-24_04-16-15_DEL_T).pdf

Basically I need the script to just leave the file name, and get rid of the
jibberish. So after running it should rename the file to:

sample pdf document.pdf

I have looked at both regular expressions and the trim function, but have
not got very far. I would really appreciate any help that people can give.

Thanks in advance.

Tao Ma

unread,
Jun 13, 2008, 4:20:32 AM6/13/08
to
Hi robm_jnb,

Try:
Get-Item *.pdf | % { Move-Item -Whatif $_ ($_.Name -replace 'pdf.*?pdf$') }

If it works like you wish, remove '-Whatif' to let it get the work done.

"robm_jnb" <robm...@discussions.microsoft.com> 写入消息新闻:16B5EFB4-CB81-4D44...@microsoft.com...

Kiron

unread,
Jun 13, 2008, 4:27:50 AM6/13/08
to
Hi Rob,
# If this suffices...
'sample pdf document.pdf_(2008-05-24_04-16-15_DEL_T).pdf'.split('_')[0]
 
# you can do:
# (using -wi to test, remove it to rename the files if satisfied)
ls $dir | ? {!$_.psIsContainer} | rni -new {$_.name.split('_')[0]} -wi
 
# or...
ls $dir *.pdf | rni -new {$_.name.split('_')[0]} -wi

--
Kiron

Kiron

unread,
Jun 13, 2008, 4:35:01 AM6/13/08
to
Oops! Bad copy 'n' paste from my part:
 
# using -Replace operator and a short RegEx
'sample pdf document.pdf_(2008-05-24_04-16-15_DEL_T).pdf' -replace '_.+$'
 
# different extensions
ls $dir | ? {!$_.psIsContainer} | rni -new {$_.name -replace '_.+$'} -wi
 
# only PDF files
ls $dir *.pdf | rni -new {$_.name -replace '_.+$'} -wi

--
Kiron

robm_jnb

unread,
Jun 13, 2008, 4:57:02 AM6/13/08
to
That was a great help, thanks for that. I have changed the script slightly:

Get-ChildItem -recurse -include *.* | % { Rename-Item -WhatIf $_($_.Name
-replace 'pdf.*?pdf$') }

Do you know if it would be possible to extract the file extension off the
end of the file name? Due to the files being renamed are of different
extensions, it would be handy if the script logically knew the extension and
stored it into a variable. So, does anyone know how to extract the last 4
characters and store it in a variable?

"Tao Ma" wrote:

> Hi robm_jnb,
>
> Try:
> Get-Item *.pdf | % { Move-Item -Whatif $_ ($_.Name -replace 'pdf.*?pdf$') }
>
> If it works like you wish, remove '-Whatif' to let it get the work done.
>

> "robm_jnb" <robm...@discussions.microsoft.com> дÈëÏûÏ¢ÐÂÎÅ:16B5EFB4-CB81-4D44...@microsoft.com...

Tao Ma

unread,
Jun 13, 2008, 5:38:43 AM6/13/08
to
Hi,

$_.Extension contains the file extension.
$_.BaseName contains the filename without extension.
I assume that there is only one 'dot' in the BaseName.

Before removing '-Whatif' parameter, please check the outputs carefully.

Get-ChildItem -Recurse | ?{ ! $_.PSIsContainer } | %{ Rename-Item -Whatif
$_.FullName ( ($_.BaseName -replace '\.[^.]*$') + $_.Extension) }

Tao Ma

"robm_jnb" <rob...@discussions.microsoft.com> 写入消息新闻:919D2E64-F683-4655...@microsoft.com...


> That was a great help, thanks for that. I have changed the script
> slightly:
>
> Get-ChildItem -recurse -include *.* | % { Rename-Item -WhatIf $_($_.Name
> -replace 'pdf.*?pdf$') }
>
> Do you know if it would be possible to extract the file extension off the
> end of the file name? Due to the files being renamed are of different
> extensions, it would be handy if the script logically knew the extension
> and
> stored it into a variable. So, does anyone know how to extract the last 4
> characters and store it in a variable?
>
> "Tao Ma" wrote:
>
>> Hi robm_jnb,
>>
>> Try:
>> Get-Item *.pdf | % { Move-Item -Whatif $_ ($_.Name -replace
>> 'pdf.*?pdf$') }
>>
>> If it works like you wish, remove '-Whatif' to let it get the work done.
>>

>> "robm_jnb" <robm...@discussions.microsoft.com> D′è????¢D???:16B5EFB4-CB81-4D44...@microsoft.com...

robm_jnb

unread,
Jun 13, 2008, 6:23:00 AM6/13/08
to
Thanks for your reply - I have taken your script and modified it to this:

get-childitem -recurse | where-object {$_.Name -match "_DEL_T"} | ?
{!$_.psIsContainer} | rni -new {$_.name -replace "_\(.+$"} -wi

Thanks for all your help - I couldn't have done it without you.

"Tao Ma" wrote:

> Hi,
>
> $_.Extension contains the file extension.
> $_.BaseName contains the filename without extension.
> I assume that there is only one 'dot' in the BaseName.
>
> Before removing '-Whatif' parameter, please check the outputs carefully.
>
> Get-ChildItem -Recurse | ?{ ! $_.PSIsContainer } | %{ Rename-Item -Whatif
> $_.FullName ( ($_.BaseName -replace '\.[^.]*$') + $_.Extension) }
>
> Tao Ma
>

> "robm_jnb" <rob...@discussions.microsoft.com> дÈëÏûÏ¢ÐÂÎÅ:919D2E64-F683-4655...@microsoft.com...


> > That was a great help, thanks for that. I have changed the script
> > slightly:
> >
> > Get-ChildItem -recurse -include *.* | % { Rename-Item -WhatIf $_($_.Name
> > -replace 'pdf.*?pdf$') }
> >
> > Do you know if it would be possible to extract the file extension off the
> > end of the file name? Due to the files being renamed are of different
> > extensions, it would be handy if the script logically knew the extension
> > and
> > stored it into a variable. So, does anyone know how to extract the last 4
> > characters and store it in a variable?
> >
> > "Tao Ma" wrote:
> >
> >> Hi robm_jnb,
> >>
> >> Try:
> >> Get-Item *.pdf | % { Move-Item -Whatif $_ ($_.Name -replace
> >> 'pdf.*?pdf$') }
> >>
> >> If it works like you wish, remove '-Whatif' to let it get the work done.
> >>

> >> "robm_jnb" <robm...@discussions.microsoft.com> D¡ä¨¨????¡éD???:16B5EFB4-CB81-4D44...@microsoft.com...

itsalla...@gmail.com

unread,
Apr 25, 2017, 2:00:55 PM4/25/17
to

you can try "KrojamSoft: BatchRenameFiles Tool". i've used it so many times for this familiar work..

Jürgen Exner

unread,
Apr 25, 2017, 2:26:54 PM4/25/17
to
On Tue, 25 Apr 2017 11:00:53 -0700 (PDT), itsalla...@gmail.com wrote
in microsoft.public.windows.powershell:

>you can try "KrojamSoft: BatchRenameFiles Tool". i've used it so many times for this familiar work..

And you truely believe that almost 9 years later the OP is still looking
for an answer? Or is still looking at this NG at all for that matter?

jue
0 new messages