Saturday, April 26, 2008

Powershell Ripped Media Renaming Script

I recently bought a book on CD and ripped it to .wma so that I could listen to it on my portable media player. When the tracks rip to disk they get named with the track number starting first. I wanted to rename the files so that the two digit disc number preceded the track number so that I could sort all of the tracks in one folder and listen to them in the correct order. Here's a Powershell script that I ran from the folder one below the ripped discs' folders.

# change directory into root of ripped files
cd "D:\Audio\Author Name"
$dirr = "BookName Disc "
$discs = 1..7 # Change to number of discs

foreach($disc in $discs)
{
    $discdir = $dirr + $disc.ToString()
    cd $discdir
    $dir = get-childitem *.wma
    foreach($x in $dir)
    {
        $newname = "0" + $disc.ToString() + "." + $x.Name.SubString(0,2) + ".wma"
        Rename-Item $x $newname
    }
    cd ..
}

I've updated the code that appeared here before 8 July 2010. Previously it did not have the loop for each disc. This code still isn't perfect. Instead of prepending the 0 to the disc number when creating the new name I should be formatting the disc value.

No comments:

Post a Comment