I am trying to move files from DIR1 to DIR2 using Perl script. My code compiles but unfortunately is not working properly. Thanks in advance for suggestions
#!/usr/bin/perl -w
use File::Copy;
use Cwd 'abs_path';
if ( @ARGV != 2 ) {
die "Script takes two parameters: dir1 dir2";
}
if ( -d $ARGV[0] && -d $ARGV[1] )
{
opendir my $DIR, $ARGV[0] or die "Read error: $!";
while(my $file = readdir $DIR )
{
next if ($file eq "." or $file eq "..");
my $filepath = abs_path($file);
print "$filepath\n";
move $filepath, $ARGV[1];
}
closedir $DIR;
}
else
{
print "Both arguments must be directories!\n";
}
Script prints all files in DIR1 but move fails.
readdir
returns only the filenames in the given directory. Thus to get an absolute filepath you need to prepend $ARGV[0].
See the perldoc for readdir for detailed information.