Listing MP3 directories. |
|||
![]() |
|||
|
This script is for producing a TAB separated list of MP3 files, designed for ease of import into a database. The names of directories and files will be output to a file of this format:
Filesize TAB Artist Name TAB Song Title TAB Path To File
Like this:
5933675 Psychedelic Furs India \\mp3\Groups\Psychedelic Furs\
5338563 Psychedelic Furs Sister Europe \\mp3\Groups\Psychedelic Furs\ 3234566 Psychedelic Furs We Love You \\mp3\Groups\Psychedelic Furs\ The MP3 files are named with a 'SPACE DASH SPACE' separating Artist and Title. This pattern is used nowhere else in the file (or directory) names. The 'Artist/Title' separator and MP3 extension are stripped to display plain text titles. The example here is split into two; the recusive loop and a subroutine to handle each line of input. The structure of the main loop is a common UNIX method which uses the output of the "ls" command within the main loop. This loop just uses readdir(DIR) to create an array of file and directory names, and loops through that instead. When it finds a name it can't process as a directory, it sends it off to the subroutine to be processed as a string that contains the full path and filename, so the subroutine could just be a print statement, or could be the focus of many string handling excersises.
The (very much simplified) Script
#!perl
use File::stat;
$dir = "\\\\mediabox\\mp3_1";
open (OUTFILE, ">/mp3/mp3z.csv");
print (OUTFILE "Bytes\tArtist\tTitle\tLocation\n");
&searchDirectory($dir);
close (OUTFILE);
sub searchDirectory {
local($dir);
local(@lines);
local($subdir);
local($lvl_counter);
local($list_length);
$dir = $_[0];
if(opendir (DIR, $dir))
{
@lines = readdir (DIR);
closedir (DIR);
$lvl_counter = 2;
$list_length = ( scalar @lines );
while ($lvl_counter < $list_length)
{
$subdir = $dir."\\".$lines[$lvl_counter];
if(opendir (SUBDIR, $subdir))
{
&searchDirectory($subdir);
closedir (SUBDIR);
}
else
{
&processnames($subdir);
}
$lvl_counter++;
}
}
}
sub processnames {
# Excuse me while I count every line.
$filecount++;
# Set a simple counter to zero.
$counter = 0;
# Give the value passed to this sub a sensible name.
$fullsongnameandpath = $_[0];
# Stat the filesize.
$file_size = stat("$fullsongnameandpath")->size;
# Split the line into useful parts.
@nameandpathdata = split(/\\/, $fullsongnameandpath);
# ...and measure the length of the array.
$nameandpathdata_length = ( scalar @nameandpathdata );
# Prepare the hyperlink text (the filename
# part of the full name and path)
$hyperlink = ($nameandpathdata[$nameandpathdata_length-1]);
# ...and trim the mp3 extension.
$hyperlink =~ s/.[M,m][P,p]3//;
# The database requires separate artist-name
# and song-title parts, so split what's left.
@songparts = split(/ - /, $hyperlink);
# Print the filesize, artistname and songname
# to the CSV file, separated by TABs
print (OUTFILE "$file_size\t$songparts[0]\t$songparts[1]\t");
# ...and loop through the 'nameandpathdata' array
# printing the path parts to the CSV list.
while ($counter < $nameandpathdata_length-1)
{
print (OUTFILE "$nameandpathdata[$counter]\\");
$counter++;
}
# Print a newline.
print (OUTFILE "\n");
}
The text-format tab seperated list can be imported into an Access database, whichis the subject of a separate ASP exercise. The ASP front end is achieved using a single tutorial from Learn ASP com, and now looks like this:
Recursive Directory Listing | Windows Favorites to HTML Back to chipwork.com. |
|||