http://www.perlmonks.org?node_id=1022078

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

I'm not a Perl programmer, but I use Perl scripts I find online to help me with scheduled processes on my computers. With that in mind, I've used scripts that mimic MS DOS' tree command, I've found scripts that read MP3 ID3 tags, and I've found scripts that read the file contents of archive files, but I'm not sure how to combine all these functions into one powerful script. So I'm hoping somebody here has the time to draft up a script that does the following...

High-Level Overview:

(1) Looking for a Perl script that can be run from MS DOS prompt on WinXP SP2 PC.

(2) Script would act like DOS Tree command for a specified directory, printing structure of sub-directory and files to a specified .txt file.

(3) For any .mp3 file, append ID3 data from the file to the output line.

(4) Treat any archive file (.zip, .rar, .gz) like a sub-directory, so that archive content is printed to output file.


Detailed Requirements:

BR01 -- Shall run script on WinXP SP2 PC with ActivePerl installed.

BR02 -- Shall run script from MS DOS prompt using Perl.

BR03 -- Shall be able to state the directory location to be parsed as a parameter in DOS command.

BR04 -- Shall be able to state the directory location and file name of the output file as a parameter in DOS command.

BR05 -- Output from script shall be similar in format to output of MS DOS Tree command to a .txt file.

BR06 -- Script shall output structure of all sub-directories and filenames within provided directory.

BR07 -- Sub-directories in each parent directory should be sorted in alphabetical order in the output file.

BR08 -- Filenames in each sub-directory should be sorted in alphabetical order in the output file.

NOTE: MS DOS Tree command does not always sort alphabetically, which this script should solve for.

BR09 -- Filenames shall include file extensions.

BR10 -- If file has file extension of .mp3, script shall append ID3 data from the .mp3 file to the output line for that file.

BR11 -- ID3 data shall include, at a minimum: song, artist, album, year, and genre.

BR12 -- ID3 data that would be nice to include would track number and disc number.

BR13 -- ID3 data should be separated by pipes (|), but be displayed on the same output line as the filename.

NOTE: I was looking at MP3::Tag module to help accomplish this.

BR14 -- Would be a nice to have if the ID3 values to output could be specified in DOS command.

BR15 -- Script shall read contents of archive files, at a minimum those archive files with file extensions of .zip, .rar, or .gz.

BR16 -- Would be a nice to have if the archive file extensions could be specified in DOS command.

BR17 -- Script shall output contents of archive file as child branches to the archive filename, including sub-directory structures and filenames.

BR18 -- ID3 requirements shall be applied to files within archive files.

BR19 -- Would be a nice to have if files could be excluded from output by specifying file extensions in DOS command.


Sample DOS command to kick off script:

script.exe "C:\location\to\parse\" "C:\location\of\output.txt" ".zip/.rar/.gz" ".not/.the/.se/.file/.ext"


Sample data in directory structure for script to parse:

C:\LocationToParse
|    
| SongFile04-01-01.mp3
| SongFile04-01-02.mp3
| SongFile04-01-03.mp3
|    
+-ArtistFolder01
| \-AlbumFolder01-01
|    ArchiveFile01-01-01.zip
|    
+-ArtistFolder02
| \-AlbumFolder02-01
|    ArchiveFile02-01-01.rar
| \-AlbumFolder02-02
|    SongFile04-02-01.mp3
|    SongFile04-02-02.mp3
|    SongFile04-02-03.mp3
|    
+-ArtistFolder03
| ArchiveFile03-01.gz


Sample output file from using script:

C:\LocationToParse
|    
| SongFile04-01-01.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
| SongFile04-01-02.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
| SongFile04-01-03.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
| NonSongFile01.txt
| NonSongFile02.txt
|    
+-ArtistFolder01
| \-AlbumFolder01-01
|   \-ArchiveFile01-01-01.zip
|      FileFromArchive01-01-01.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|      FileFromArchive01-01-02.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|      FileFromArchive01-01-03.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|      NonSongFile01.txt
|    
+-ArtistFolder02
| \-AlbumFolder02-01
|   \-ArchiveFile02-01-01.rar
|     \-FolderFromArchive02
|       \-SubFolderFromArchive02-01
|          FileFromArchive01-01-01.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|          FileFromArchive01-01-02.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|          FileFromArchive01-01-03.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|          NonSongFile01.txt
| \-AlbumFolder02-02
|    SongFile04-02-01.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|    SongFile04-02-02.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|    SongFile04-02-03.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|    NonSongFile01.txt
|    
+-ArtistFolder03
| \-ArchiveFile03-01.gz
|   \-FolderFromArchive02
|     \-SubFolderFromArchive02-01
|        FileFromArchive01-01-01.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|        FileFromArchive01-01-02.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|        FileFromArchive01-01-03.mp3 | ID3Song | ID3Album | ID3Artist | ID3Year | ID3Genre
|        NonSongFile01.txt

Thanks in advance!
  • Comment on Directory tree script that reads ID3 tags and archive file contents

Replies are listed 'Best First'.
Re: Directory tree script that reads ID3 tags and archive file contents
by bart (Canon) on Mar 06, 2013 at 20:14 UTC
    You should be able to put something together with File::Find (or one of its derivatives, if you prefer that), MP3::Tag or one of its competitors, and Archive::Zip.

    The core of the script could be like this:

    use File::Find; @ARGV = '.' unless @ARGV; find sub { # all code comes here return unless -f; # $_ is basename, use this because we have chdir'ed to its directo +ry if(/\.txt$/) { say $File::Find::name; } elsif(/\.zip$/) { # process ZIP file } elsif(/\.mp3$/) { # process mp3 file } }, @ARGV;
Re: Directory tree script that reads ID3 tags and archive file contents
by Anonymous Monk on Mar 07, 2013 at 09:46 UTC

    I'm not a Perl programmer, but I use Perl scripts I find online...

    Which ones? Link them and read perlintro

    Detailed Requirements:

    Neato, how did you come up with that? Where is this job posting?