Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Batch rename of files

Reply
 
Old 06-13-2008   #1 (permalink)
robm_jnb


 
 

Batch rename of files

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.

My System SpecsSystem Spec
Old 06-13-2008   #2 (permalink)
Tao Ma


 
 

Re: Batch rename of files

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_jnb@xxxxxx> 写入消息新闻:16B5EFB4-CB81-4D44-BFEB-1B31E9B44B5F@xxxxxx
Quote:

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

My System SpecsSystem Spec
Old 06-13-2008   #3 (permalink)
Kiron


 
 

Re: Batch rename of files

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
My System SpecsSystem Spec
Old 06-13-2008   #4 (permalink)
Kiron


 
 

Re: Batch rename of files

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
My System SpecsSystem Spec
Old 06-13-2008   #5 (permalink)
robm_jnb


 
 

Re: Batch rename of files

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:
Quote:

> 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_jnb@xxxxxx> 脨麓脠毛脧没脧垄脨脗脦脜:16B5EFB4-CB81-4D44-BFEB-1B31E9B44B5F@xxxxxx
Quote:

> > 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.
>
>
>
My System SpecsSystem Spec
Old 06-13-2008   #6 (permalink)
Tao Ma


 
 

Re: Batch rename of files

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" <robmjnb@xxxxxx> 写入消息新闻:919D2E64-F683-4655-AEC8-FF084F0C7FC0@xxxxxx
Quote:

> 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:
>
Quote:

>> 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_jnb@xxxxxx> D′è????¢D???:16B5EFB4-CB81-4D44-BFEB-1B31E9B44B5F@xxxxxx
Quote:

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

My System SpecsSystem Spec
Old 06-13-2008   #7 (permalink)
robm_jnb


 
 

Re: Batch rename of files

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:
Quote:

> 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" <robmjnb@xxxxxx> 脨麓脠毛脧没脧垄脨脗脦脜:919D2E64-F683-4655-AEC8-FF084F0C7FC0@xxxxxx
Quote:

> > 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:
> >
Quote:

> >> 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_jnb@xxxxxx> D隆盲篓篓????隆茅D???:16B5EFB4-CB81-4D44-BFEB-1B31E9B44B5F@xxxxxx
> >> > 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.
> >>
> >>
> >>
>
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
In Vista, can .wps files be converted to .rtf files as a batch i.e.instead of one at a time? Vista General
Rename a batch of file in sequence Vista General
UAC and batch files Vista General
rename all files from a directory to a list of files ... PowerShell
rename files (lots of files for newbie) PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46