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 - renaming files, depending on their dates ...

Reply
 
Old 09-27-2007   #1 (permalink)
show3r


 
 

renaming files, depending on their dates ...

hi,

i have a list of mp3 files in non-alphabetical order,

i want to sort the ascending on the property "date modified"

i want to add a number, so that the mp3s are sorted correctly on my
mp3 player as

jamming.mp3
gone with the wind.mp3
zero clutter - fast rush.mp3

renamed to

9999 gone with the wind.mp3
9998 zero clutter.mp3
9997 jamming.mp3

also the filename, after the new added number (from 9999 to 00000)
should be limited to 26 chars,
how is that posible?

cincerly yours - $how3r


My System SpecsSystem Spec
Old 09-27-2007   #2 (permalink)
Brandon Shell


 
 

Re: renaming files, depending on their dates ...

Try this...
$i=1
get-childitem c:\test -rec | Sort-Object LastWriteTime | foreach{$newname =
"$i-{0}" -f $_.Name; Rename-Item $_.Fullname -new $newName -whatif; $i++}

If it works as expected remove whatif

"show3r" <show3r@xxxxxx> wrote in message
news:1190899384.922672.14830@xxxxxx
Quote:

> hi,
>
> i have a list of mp3 files in non-alphabetical order,
>
> i want to sort the ascending on the property "date modified"
>
> i want to add a number, so that the mp3s are sorted correctly on my
> mp3 player as
>
> jamming.mp3
> gone with the wind.mp3
> zero clutter - fast rush.mp3
>
> renamed to
>
> 9999 gone with the wind.mp3
> 9998 zero clutter.mp3
> 9997 jamming.mp3
>
> also the filename, after the new added number (from 9999 to 00000)
> should be limited to 26 chars,
> how is that posible?
>
> cincerly yours - $how3r
>
My System SpecsSystem Spec
Old 09-27-2007   #3 (permalink)
Brandon Shell


 
 

Re: renaming files, depending on their dates ...

With all the discussion about one liners.. I figure I would get you the
verbose output

# Set Counter
$i=1
# Gets files in Path and sorts by LastWriteTime (modified)
$files = get-ChildItem c:\test | Where-Object{$_.PSisContainer -eq $false} |
Sort-Object LastWriteTime
# process all the files
foreach($file in $files)
{
# Creates a New Name adding $i- to the front
$newname = "$i-{0}" -f $file.Name
# Renames
Rename-Item $file.Fullname -new $newName -whatif
# increase counter
$i++
}


"Brandon Shell" <tshell.mask@xxxxxx> wrote in message
news:OAyiLURAIHA.3900@xxxxxx
Quote:

> Try this...
> $i=1
> get-childitem c:\test -rec | Sort-Object LastWriteTime | foreach{$newname
> = "$i-{0}" -f $_.Name; Rename-Item $_.Fullname -new $newName -whatif;
> $i++}
>
> If it works as expected remove whatif
>
> "show3r" <show3r@xxxxxx> wrote in message
> news:1190899384.922672.14830@xxxxxx
Quote:

>> hi,
>>
>> i have a list of mp3 files in non-alphabetical order,
>>
>> i want to sort the ascending on the property "date modified"
>>
>> i want to add a number, so that the mp3s are sorted correctly on my
>> mp3 player as
>>
>> jamming.mp3
>> gone with the wind.mp3
>> zero clutter - fast rush.mp3
>>
>> renamed to
>>
>> 9999 gone with the wind.mp3
>> 9998 zero clutter.mp3
>> 9997 jamming.mp3
>>
>> also the filename, after the new added number (from 9999 to 00000)
>> should be limited to 26 chars,
>> how is that posible?
>>
>> cincerly yours - $how3r
>>
>
My System SpecsSystem Spec
Old 09-27-2007   #4 (permalink)
Kiron


 
 

Re: renaming files, depending on their dates ...

# files with '[' or ']' in their names "can't be found" with Rename-Item
# remove the -whatIf switch to apply changes\

$music = resolve-path "~\my documents\my music"
$original = get-childItem $music *.mp3 -recurse | where {!$_.psIsContainer}
| sort LastWriteTime | select fullname
$ofs=''
$counter = 9999
$new = $original | foreach {"{0:0000} {1}" -f $counter, ((split-path
$_.fullname -leaf) -replace '\.mp3$'); $counter--} | foreach
{"$($_[0..21]).mp3"}
if ($original.length -eq $new.length)
{
0..($new.length - 1) | % {rename-item $original[$_].fullname
$new[$_] -whatIf}
}

--
Kiron

My System SpecsSystem Spec
Old 09-28-2007   #5 (permalink)
Kiron


 
 

Re: renaming files, depending on their dates ...

This time al+AF8- files are renamed, even those with brackets:

+ACM- get the music folder's path
+ACQ-music +AD0- resolve-path +ACIAfgBc-my documents+AFw-my music+ACI-

+ACM- get the sorted list of the songs
+ACM- filter files only (no directories)
+ACQ-original +AD0- get-childItem +ACQ-music +ACo-.mp3 -recurse +AHw-
where +AHsAIQAkAF8-.psIsContainer+AH0- +AHw- sort lastWriteTime

+ACM- change the Output Field Separator
+ACQ-ofs +AD0- ''

+ACQ-counter +AD0- 9999

+ACM- create a list of new names from the first list
+ACQ-new +AD0- +ACQ-original +AHw- foreach +AHs-
+ACM- create the new name in the form of
+ACM- +ADw-counter+AD4APA-space+AD4APA-name+AD4- without the .mp3 extension
+ACM- and decrement the counter
+ACIAew-0:0000+AH0- +AHs-1+AH0AIg- -f +ACQ-counter--,
((split-path +ACQAXw-.fullname -leaf) -replace '+AFw-.mp3+ACQ-')
+AH0- +AHw- foreach +AHs-
+ACM- limit the size of the new name to 22 chars
+ACM- and append the .mp3 extension (+-4 chars)
+ACM- filename's length should be -le 26 chars
+ACM- here is why the +ACQ-ofs was changed to an empty string
+ACIAJA-(+ACQAXwBb-0..21+AF0-).mp3+ACI-
+AH0-

+ACM- iterate through all elements and create a new list with
+ACM- escaped brackets in order for Rename-Item to rename all files
+ACQ-original+AF8- +AD0- +ACQ-original +AHw- foreach +AHsAJABf-.fullName -replace '+AFsAXABbAFwAXQBd-', '+AGAAYAAk-0'+AH0-

+ACM- verify that both lists are of the same size
if (+ACQ-original+AF8-.length -eq +ACQ-new.length) +AHs-
+ACM- pipe the lists' indices
0..(+ACQ-new.length - 1) +AHw- foreach +AHs-
+ACM- rename each file, +AF8-remove+AF8- -whatIf if trial run succeeds
rename-item +ACQ-original+AF8AWwAkAF8AXQ- +ACQ-new+AFsAJABfAF0- -whatIf
+AH0-
+AH0-

+ACM- restore the Output Field Separator to a space
+ACQ-ofs +AD0- ' '

--
Kiron

My System SpecsSystem Spec
Old 09-28-2007   #6 (permalink)
Kiron


 
 

Re: renaming files, depending on their dates ...

Correction: Because of Rename-Item's bug, execute the script from a drive
different from the one that contains the target files. This will ensure the
renaming of files whose path or name contains brackets.

Here is the one-line version:

+ACM- long version
+ACQ-ofs +AD0- ''+ADs- +ACQ-counter +AD0- 9999+ADs- get-childItem +ACIAfgBc-my documents+AFw-my music+ACI-
+ACo-.mp3 -recurse +AHw- where +AHsAIQAkAF8-.psIsContainer+AH0- +AHw- sort lastWriteTime +AHw- foreach
+AHs-add-member scriptProperty new +AHsAIgB7-0:0000+AH0- +AHs-1+AH0-.mp3+ACI- -f +ACQ-counter,
+ACIAJA-(+AGAAIgAk-(+ACQ-this.name -replace '+AFw-.mp3+ACQ-')+AGAAIgBb-0..16+AF0-)+ACIAfQ- -in +ACQAXw- -pass+ADs- +ACQ-counter--+AH0- +AHw-
foreach +AHs-rename-item (+ACQAXw-.fullName -replace '+AFsAXABbAFwAXQBd-', '+AGAAYAAk-0') +ACQAXw-.new -whatIf+AH0AOw-
+ACQ-ofs +AD0- ' '

+ACM- short version
+ACQ-ofs+AD0-''+ADsAJA-counter+AD0-9999+ADs-ls +ACIAfgBc-my documents+AFw-my
music+ACIAKg-.mp3 -r+AHw-?+AHsAIQAkAF8-.psIsContainer+AH0AfA-sort lastWriteTime+AHwAJQB7-add-member
scriptProperty new +AHsAIgB7-0:0000+AH0-
+AHs-1+AH0-.mp3+ACI--f+ACQ-counter,+ACIAJA-(+AGAAIgAk-(+ACQ-this.name-replace'+AFw-.mp3+ACQ-')+AGAAIgBb-0..16+AF0-)+ACIAfQ--in
+ACQAXw- -pass+ADsAJA-counter--+AH0AfAAlAHs-rni(+ACQAXw-.fullName-replace'+AFsAXABbAFwAXQBd-','+AGAAYAAk-0')+ACQAXw-.new-wh+AH0AOwAk-ofs+AD0-'
'

--
Kiron

My System SpecsSystem Spec
Old 09-28-2007   #7 (permalink)
Kiron


 
 

Re: renaming files, depending on their dates ...

My last two postings had wrong Encoding for the web, sorry.
.................................................................................

Because of Rename-Item's bug, execute the script from a drive
different from the one that contains the target files. This will ensure the
renaming of files whose path or name contains brackets.

# get the music folder's path
$music = resolve-path "~\my documents\my music"

# get the sorted list of the songs
# filter files only (no directories)
$original = get-childItem $music *.mp3 -recurse |
where {!$_.psIsContainer} | sort lastWriteTime

# change the Output Field Separator
$ofs = ''

$counter = 9999

# create a list of new names from the first list
$new = $original | foreach {
# create the new name in the form of
# <counter><space><name> without the .mp3 extension
# and decrement the counter
"{0:0000} {1}" -f $counter--,
((split-path $_.fullname -leaf) -replace '\.mp3$')
} | foreach {
# limit the size of the new name to 22 chars
# and append the .mp3 extension (+4 chars)
# filename's length should be -le 26 chars
# here is why the $ofs was changed to an empty string
"$($_[0..21]).mp3"
}

# iterate through all elements and create a new list with
# escaped brackets in order for Rename-Item to rename all files
$original_ = $original | foreach {$_.fullName -replace '[\[\]]', '``$0'}

# verify that both lists are of the same size
if ($original_.length -eq $new.length) {
# pipe the lists' indices
0..($new.length - 1) | foreach {
# rename each file, _remove_ -whatIf if trial run succeeds
rename-item $original_[$_] $new[$_] -whatIf
}
}

# restore the Output Field Separator to a space
$ofs = ' '

.................................................................................

Here is the one-line version:

# long version
$ofs = ''; $counter = 9999; get-childItem "~\my documents\my music"
*.mp3 -recurse | where {!$_.psIsContainer} | sort lastWriteTime | foreach
{add-member scriptProperty new {"{0:0000} {1}.mp3" -f $counter,
"$(`"$($this.name -replace '\.mp3$')`"[0..16])"} -in $_ -pass; $counter--} |
foreach {rename-item ($_.fullName -replace '[\[\]]', '``$0') $_.new -whatIf};
$ofs = ' '

# short version
$ofs='';$counter=9999;ls "~\my documents\my
music"*.mp3 -r|?{!$_.psIsContainer}|sort lastWriteTime|%{add-member
scriptProperty new {"{0:0000}
{1}.mp3"-f$counter,"$(`"$($this.name-replace'\.mp3$')`"[0..16])"}-in
$_ -pass;$counter--}|%{rni($_.fullName-replace'[\[\]]','``$0')$_.new-wh};$ofs='
'

--
Kiron

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
assigning random creation dates to files PowerShell
backup and restore files depending on Vista version Vista security
Copying files with Creation Dates preserved? Vista General
Renaming Files Vista General
Searching for Files Modified between two dates Vista file management


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