in reply to Trying to find and copy mp3s

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: $!"; } }

Replies are listed 'Best First'.
Re^2: Trying to find and copy mp3s
by Anonymous Monk on Feb 12, 2007 at 00:55 UTC
    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: $!";
Re^2: Trying to find and copy mp3s
by dsheroh (Monsignor) on Feb 12, 2007 at 15:48 UTC
    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.

        OK, fair enough. I just wanted to be sure there wasn't some difference I was missing by habitually using only -w.