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

use File::Copy; my ($filetobecopied,$newfile); system('ls -lrt | tail -15 | grep working > temp.txt'); open(FH,"<temp.txt"); open(FH1,">final.txt"); while(<FH>) { my $line = $_; if($line=~/.*(L_.*)(\d{1})(\.txt)(\.working)/) { # print "$1 - $2 - $3 - $4 \n"; my $one = $1; my $two = $2; my $three = $3; my $four = $4; my $val = 1; #print "$one 1 $three \n"; my $append = "$one" . '1' . "$three"; print "========================================== \n"; print "$append \n"; $filetobecopied = $append; $newfile = "/u/mindspring/data/fulfillment/FSI/outgoin +g/ $append"; copy("$filetobecopied","$newfile") or die "File cannot + be copied."; # The path of the existing directory where the file L_push_201207191101.txt to be copied is /u/mindspring/data/fulfillm +ent/FSI/outgoing } }

The error i am getting is File cannot be copied. at ClientLogic.pl line 21, <FH> line 1. So, how can a file created from a perl script be copied in the destination folder ?

Thanks in advance Pavan

Replies are listed 'Best First'.
Re: How to copy a file in a perl variable to a directory
by choroba (Cardinal) on Jul 20, 2012 at 13:45 UTC
    The error you are getting is not informative. Add $! to the die string so the reason of the failure is made clear.
      Good advice. Since the arguments to copy() are also variables I would always include them in the die string too just to make sure they contain what you think they do.
      While it is certainly advisable to output $!, it is not always clear whether the reported error relates to the source- or the destination file. This is particularily true in case the code is supposed to run on Windows, where I found that many kinds of access problems lead to the same error string.

      I would therefore also do some basic checks before calling copy, at least: read permission on the source file, write permission for the destination directory.

      -- 
      Ronald Fischer <ynnor@mm.st>
Re: How to copy a file in a perl variable to a directory
by 2teez (Vicar) on Jul 20, 2012 at 22:48 UTC
    Hi pavanpvss,

    I think you have to re - read the File::Copy module documentation again. However, before you do that the following adjustment could make your code work perfectly

    use File::Copy; my ($filetobecopied,$newfile); system('ls -lrt | tail -15 | grep working > temp.txt'); open FH,"<temp.txt" or die "can't open: $!"; open FH1,">final.txt" or die "can't open: $!"; while(<FH>) { my $line = $_; if($line=~/.*(L_.*)(\d{1})(\.txt)(\.working)/) { # print "$1 - $2 - $3 - $4 \n"; my $one = $1; my $two = $2; my $three = $3; my $four = $4; my $val = 1; print FH1 $one , '1' , $three,"\n============================== +============ \n"; } } $filetobecopied = "final.txt"; close FH1 or die "can't close:$!"; close FH or die "can't close: $!"; $newfile = "/u/mindspring/data/fulfillment/FSI/outgoing/"; copy($filetobecopied,$newfile) or die "File cannot be copied.";

    This should work fine. The Principle is simple, open two file handlers, FH for input and FH1 for output, using the while loop, go over each line get and analysis lines that meet your condition in the "if condition". Print to the file handler FH1, which output to a file named "final.txt". Assign final.txt to a variable name, $filetobecopied, which is evetually copied to $newfile!

    Now you can go over File::Copy module Documentation Again.

    Hope this helps.