This script assumes you are running on a unixy-type system

It also assumes you are willing to edit the script every time you want to run it on some different directory (other than "/steeler/mp3", which doesn't exist on most "unixy-type" systems ;)

And it assumes that if the directory contains any mp3 file, then it contains nothing but mp3 files -- all files get listed in your "m3u" file (including the m3u file, if the script has been run previously on that path).

And it assumes that there will never be a problem with opening "m3u" files for output, or that you don't care when open fails.

There's no need to make any of these assumptions (and no need for chdir):

#!/usr/bin/perl use strict; use warnings; use File::Find::Rule; use File::Spec; die "Usage: $0 path/to/search\n" unless ( @ARGV == 1 and -d $ARGV[0] ) +; my @dirs = File::Find::Rule->directory()->in( $ARGV[0] ) or die "Can't find anything in $ARGV[0]\n"; for my $dir ( @dirs ) { my @mp3s = File::Find::Rule->maxdepth(1)->file()->name("*.[Mm][Pp] +3")->in($dir); if ( @mp3s ) { ( my $m3u = ( File::Spec->splitdir( $dir ))[-1] ) =~ s/[\s.']+ +//g; if ( open( M3U, ">", "$dir/$m3u.m3u" )) { print M3U "$_\n" for ( @mp3s ); close M3U; warn sprintf( "%d mp3s listed in %s\n", scalar @mp3s, "$di +r/$m3u.m3u" ); } else { warn "Unable to create $dir/$m3u.m3u : $!\n"; } } }
Updated second call to File::Find::Rule so that it matches *.MP3 as well as *.mp3 (and *.mP3 and *.Mp3). Also fixed syntax error (added missing quotes in sprintf arg).

In reply to Re: Generate m3u files automatically by graff
in thread Generate m3u files automatically by rementis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.