HeyYou has asked for the wisdom of the Perl Monks concerning the following question:

when I go to run the program I relize that its not getting to the second foreach loop. Any ideas? and does my code look like it will work?
#!/usr/bin/perl -w #mp3Finder.pl use strict; use File::Copy; for(1...262) { print $_; my $dir = "/media/second/testdisk/linux/recup_dir.$_"; print $dir; chdir($dir); my @mp3s = glob "*.mp3"; for(@mp3s) { my $music = "/home/zack/music/$_"; copy($_, $music); } }

Replies are listed 'Best First'.
Re: Trying to find and copy mp3s
by Joost (Canon) on Feb 11, 2007 at 19:43 UTC
    well either there are no dirs named /media/second/testdisk/linux/recup_dir.1 .. /media/second/testdisk/linux/recup_dir.262, or you don't have rx permission, or there are no files in those dirs ending in .mp3, or you don't have permission to write to /home/zack/music. this is why you should check if system calls succeed.

    try

    chdir($dir) or die "Can't chdir to $dir: $!";
    and
    copy($_,$music) or die "Can't copy $_ to $music: $!";
Re: Trying to find and copy mp3s
by kyle (Abbot) on Feb 11, 2007 at 21:56 UTC

    It's a bad idea to use $_ for two loops at once. I don't think that's what your problem is here, but it could become a maintainability problem later.

    You should check that chdir and copy succeeded before charging ahead in your loop (as already suggested by Joost).

    It's always a good idea to use warnings too (though, again, I don't think that will help in this case). [Oops, as esper points out, the OP already has -w.]

    #!/usr/bin/perl -w #mp3Finder.pl use strict; use warnings; use File::Copy; foreach my $dirnum (1 .. 262) { print $dirnum; my $dir = "/media/second/testdisk/linux/recup_dir.$dirnum"; print $dir; chdir($dir) or die "Can't chdir($dir): $!"; my @mp3s = glob "*.mp3"; foreach my $mp3 (@mp3s) { my $music = "/home/zack/music/$mp3"; copy($mp3, $music) or die "Can't copy $mp3 to $music: $!"; } }
      Its definately getting to the loops and everything, but now I get an error on the copy cammand, saying that the file I'm copying to does not exist. How can I create it?

        It's probably telling you that because the destination directory does not exist. You can create it with mkdir. For example:

        my $destination = "/home/zack/music"; my $music = "$destination/$mp3"; if ( ! -d $destination && ! mkdir $destination ) { die "Can't mkdir $destination: $!"; } copy( $mp3, $music ) or die "Can't copy $mp3 to $music: $!";
      This is the second time in recent days that I have seen use warnings suggested to someone whose code already included #!/usr/bin/perl -w. I have long been under the impression that the -w switch is equivalent to use warnings. (man perlrun states that -w "prints warnings about dubious constructs" and "really just enables the internal $^W variable", but does not explictly describe its relation to use warnings.)

      Is there an actual, functional difference between the two? If not, why the suggestion that both should be used?

        I don't know of any functional difference. I just didn't notice the -w in the OP. In my own code, I always have both:

        use strict; use warnings;
        ...just like that at the top of every file. It's such a strong habit that when I see just one of them, I think the other is missing.

        Anyway, my mistake. Thanks for pointing it out.