props has asked for the wisdom of the Perl Monks concerning the following question:
Symptom: "In the following loop only the first instance of $folder gets executed.The loop ends thereafter".
My case is similar to this thread 658908. The following script grabs all rar files from input directory then extracts each file in the output directory. Using the mp3splt command i split the mp3 files accordingly. Some folder name editing happens in the end.foreach my $folder (@folders) { next if $folder =~ /^\.\.?/; my $fqname = catdir( $output, $folder ); if ( -d $fqname ) { if ( ( glob("$fqname/*.cue") ) ) { print "$fqname: cue found\n"; } else { print "$fqname: cue found\n"; my ($cue) = <$fqname/*.cue>; my ($mp3) = <$fqname/*.mp3>; &process( $cue, $mp3, $fqname ); remove qw(*.cue *.m3u *nfo *.sfv); &renameFolder($folder);
full code
use warnings; use strict; use File::Remove qw(remove); use File::Spec::Functions; my $input = '/mnt/music/input/'; my $output = '/mnt/music/output/'; opendir DIR, $input or die "cannot open DIR: $!\n"; my @rars = grep /\.rar$/i, readdir(DIR); closedir(DIR); chdir($output) or die "error chmod to /output: $!"; foreach my $rar (@rars) { &extract($rar); } opendir( OUTPUT, $output ) or die "error open OUTPUT\n"; my @folders = readdir(OUTPUT) or die "error read folders\n"; closedir(OUTPUT); foreach my $folder (@folders) { next if $folder =~ /^\.\.?/; my $fqname = catdir( $output, $folder ); if ( -d $fqname ) { # assuming i have 2 folders with each one hosting a cue file # only the first cue file will be found if ( ( glob("$fqname/*.cue") ) ) { print "$fqname: cue found\n"; } else { print "$fqname: cue found\n"; my ($cue) = <$fqname/*.cue>; my ($mp3) = <$fqname/*.mp3>; &process( $cue, $mp3, $fqname ); remove qw(*.cue *.m3u *nfo *.sfv); &renameFolder($folder); } } } sub extract { my $file = shift; my @args = ( "unp", "$input$file" ); system(@args) == 0 or die "cannot extract: $!\n"; } sub process { my $cue = shift; my $mp3 = shift; my $folder = shift; my @args = ( "mp3splt", "-c", "$cue", "$mp3", "-o", '@n_@a_@b_@t' +); system(@args) == 0 or die "system @args failed: $?"; remove $mp3; chdir $output; } sub renameFolder { my $folder_before = shift; for ( my $folder_after = $folder_before ) { s/_-_/_/; s/-PsyCZ//; s/-UPE//; s/-mAhA//; s/-/_/; rename( $folder_before, $folder_after ); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Loop stops at first iteration
by ikegami (Patriarch) on Dec 28, 2007 at 05:39 UTC | |
by props (Hermit) on Dec 28, 2007 at 17:45 UTC | |
by ikegami (Patriarch) on Dec 29, 2007 at 04:39 UTC |