1820260 Members
2937 Online
109622 Solutions
New Discussion юеВ

Move function + Perl

 
network_4
Advisor

Move function + Perl

Hi, I am facing one issue. Below is one test script:
++++++++++++++++Script+++++++++++++++++++++++++

#!/usr/bin/perl
use File::Copy;
open ('FILE', "/var/atul/file");
@file=;
close (FILE);
$a=0;
#$temp=$#file;
#for ($i=0;$i<=3;$i++) {
foreach $file (@file) {
#move '/var/atul/atul1/atul2/$file[$a]', '/var/atul/' or die "move failed: $!";
$old = "/var/atul/atul1/atul2/$file[$a]";
$new = "/var/atul/$file[$a]";
#move $old,$new;
print "$old";
print "$new";
move $old,$new or die "move failed: $!";
$a++;
}

+++++++++++++++++++++++++++++++++++++++++++++++++
here what i am doing is taking file name from one location through variable and then putting the value of that variable in move function to move a file of same name to another location. Now thing is when i am giving print command its giving me proper file name but if i am using move function its taking that variable as filename and operation get failed.

This is error message:

+++++++++++++++++++++++++++++++++++++++++
/var/atul/atul1/atul2/file1
/var/atul/file1
Unsuccessful stat on filename containing newline at /usr/lib/perl5/5.8.0/File/Copy.pm line 185.
Unsuccessful stat on filename containing newline at /usr/lib/perl5/5.8.0/File/Copy.pm line 189.
Unsuccessful stat on filename containing newline at /usr/lib/perl5/5.8.0/File/Copy.pm line 190.
Unsuccessful stat on filename containing newline at /usr/lib/perl5/5.8.0/File/Copy.pm line 205.
Unsuccessful stat on filename containing newline at /usr/lib/perl5/5.8.0/File/Copy.pm line 92.
Unsuccessful open on filename containing newline at /usr/lib/perl5/5.8.0/File/Copy.pm line 119.
move failed: No such file or directory at test.pl line 16.
+++++++++++++++++++++++++++++++++++++++++++++++

its clear that its taking "$file[$a]" as file name not the value of this variable when using move. So please let me know how can i move a file in this case.

4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: Move function + Perl

Shalom,

Doesn't look like bad code to me.

Looks like use File::Copy; is not finding the library or doesn't have permissions to execute it.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ralph Grothe
Honored Contributor

Re: Move function + Perl

Looks to me as if you forgot to chomp the array of files read from the opened file.
How about

chomp @file;

Madness, thy name is system administration
network_4
Advisor

Re: Move function + Perl

Thanks.. Issue solved . It was due to not using chomp.
H.Merijn Brand (procura
Honored Contributor

Re: Move function + Perl

Start with checking all calls!
Of course the chomp () does the trick, but your code is flacky in many ways.


#!/usr/bin/perl
# Hmm, start using:
# use strict;
# use warnings;
use File::Copy;
# First arg should NOT be quoted!
#open ('FILE', "/var/atul/file");
my $file = "/var/atul/file";
open my $fh, "<", $file or die "$file: $!";
#@file=;
chomp (my @file = <$h>);
# close (FILE);
close $fh;

for (@file) {
# Huh? You changed the code to not use $a, and now you use it?
# $old = "/var/atul/atul1/atul2/$file[$a]";
my $old = "/var/atul1/atul2/$_";
# $new = "/var/atul/$file[$a]";
my $new = "/var/atul/$_";
# Diagnostics should go to STDERR
print STDERR "$old\n";
print STDERR "$new\n";
move $old, $new or die "move failed: $!";
}

BTW You should still consider upgrading your perl

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn