Dear Monks

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 ); } }

In reply to Loop stops at first iteration by props

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.