I find it peculiar that the file system is scanned twice. The following avoids that:

#!/usr/bin/perl use strict; use warnings; use File::Find::Rule qw( ); use Path::Class qw( file ); die "Usage: $0 path/to/search\n" if @ARGV != 1 || !-d $ARGV[0]; my @scan = File::Find::Rule ->name(qr/\.mp3\z/i) #->file ->in( $ARGV[0] ); my $last_dir = ''; my $fh; for (@scan) { my $file = file($_); my $dir = $file->dir; if ($dir ne $last_dir) { $dir = $last_dir; my $m3u = $dir->dir_list(-1); $m3u =~ s/[\s.']+//g; $m3u = $dir->file("$m3u.m3u"); open($fh, '>', $m3u) or warn("Unable to create \"$m3u\": $!\n"); } print($fh "$_\n"); }

Untested.

Note that this (and previous solutions) will probably create an m3u named "..m3u" if you pass "." for argument and that directory contains mp3s. This can be solved by changing
->in( $ARGV[0] )
to
->in( dir($ARGV[0])->absolute )
(Don't forget to import dir.)

(Should have been a reply to the OP. The OP was notified.)


In reply to Re^2: Generate m3u files automatically by ikegami
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.