in reply to Re: Generate m3u files automatically
in thread Generate m3u files automatically
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.)
|
|---|