Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Renaming Files

by Anonymous Monk
on Aug 23, 2001 at 03:35 UTC ( #107193=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do I rename a file to include the directory as part of the filename?

I have thousands of Bruce Springsteen MP3's on my PC. They are all from different live shows. They are setup in directories named for each show. However, the file names themselves do not have the artist or the concert as part of the file name.

For example, in the folder

/music/Springsteen/Springsteen - 1973-03-07 Max's Kansas City

I have several songs,

01 Mary Queen of Arkansas.mp3
02 Bishop Dance.mp3
08 Song to Orphans.mp3

Unfortunately the MP3 player I'm using only displays the file name in its playlist. Therefore identifying a particular song from a particular show is difficult. How can I rename the files to include the parent directory as part of the file name? I'm looking for a simple script that will let me do it.

Ideally my files would all be renamed like this :

Springsteen - 1973-03-07 Max's Kansas City - 01 Mary Queen of Arkansas.mp3
Springsteen - 1973-03-07 Max's Kansas City - 02 Bishop Dance.mp3
Springsteen - 1973-03-07 Max's Kansas City - 08 Song to Orphans.mp3

Can this be done?

Replies are listed 'Best First'.
Re: Renaming Files
by blakem (Monsignor) on Aug 23, 2001 at 03:43 UTC
      Thanks, but it's not quite what I'm looking for. I need a script that would start at the parent directory and recursively go into each subdirectory. While there, it would use the directory name (but not the full path) as part of the file name. It's a little more tricky than just renaming a couple files.
        Warning, untested code...

        how about something like:

        my $dir = '/my/mp3/directory'; opendir(DIR,$dir) or die "Cannot open $dir: $!"; while(defined($subdir = readdir(DIR))) { next if $subdir =~ /^\./; next unless -d "$dir/$subdir"; opendir(DIR2,"$dir/$subdir") or die "Cannot open $dir/subdir: $!"; while (defined($file = readdir(DIR2))) { next unless $file =~ /\.mp3$/i; my $src = "$dir/$subdir/$file"; my $dest = "$dir/$subdir/${subdir}_$file"; print "renaming $src => $dest\n"; # rename $src, $dest; } }


Re: Renaming Files
by Dragonfly (Priest) on Aug 23, 2001 at 13:54 UTC
    Wow, that's a lot of Bruce.

    One approach would be to write a script that went into the /music/Springsteen directory, grabbed all the folder names into an array, and then write a foreach that went into the directories one by one and prepended the directory name to the song's filename.

    You could also write a regex that would search the song names for the directory name to avoid changing filenames that were already correct.

    Sorry I can't write any example code right now, it's almost 5:00 AM here and I think any attempt I would make would be more amusing than helpful right now. ;-)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://107193]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-05-27 02:35 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (94 votes). Check out past polls.