Copy work very well, while Move doesnt work at all.

97 Views Asked by At
use File::Copy;


#Variable with my directory I work on
$dir = "C:/projekty/perl/muzyka";

#Variables used to find all mp3 files
$dir_tmp = $dir."/*.mp3";
@files = glob( $dir_tmp );

#Variable with directory I want to create and put my files to
$new_dir = "C:/projekty/perl/muzyka/new_dir";

#Creating new directory
mkdir ( $new_dir ) or print "MKDIR PROBLEM";

Till this point everything is allright. Now I put the loop:

foreach( @pliki )
{   
    copy( $_, $new_dir) or print "COPY PROBLEM";
}

or:

foreach( @pliki )
{   
    move( $_, $new_dir) or print "MOVE PROBLEM";
}

And the problem is: Copy works perfectly fine, but Move doesn't want to do its job. It works sometimes depends on some modifications in code but never in a loop. Simple code with 1 line:

move($a, $b);

works perfectly. But if I use some conditions or loops it stops working even if arguments (directories) seem OK (I checked them with print function put in a loop). Why is it not working? Are there any circumstances that would cause errors?

1

There are 1 best solutions below

0
On

copy and move are documented to set $! on error. It's also good to check whether the arguments are what you expect them to be. Pay particular attention to the presence of newlines and trailing spaces.

move($_, $new_dir)
   or warn("Can't move \"$_\" to \"$new_dir\": $!\n");