CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
failing to copy files

 



tester_V
Novice

Nov 19, 2017, 11:03 AM

Post #1 of 10 (2143 views)
failing to copy files Can't Post

Good morning!
I probably overusing my welcome here but I hope not!

I'm now having a problem copying files to a directory. I'm using array list file.
I tried "foreach' loop and "while" loop but they both producing same error'

Could anyone (Laurent?) point me to a right direction why I'm falling to copy files?
Here is the code:

my $ndirname1 = "C:/Sixshot_logs/Flow/";
mkdir $ndirname1 ;

my $list = "C:/Sixshot_logs/Flow-Sorted.txt" ;
open (my $filelist_fh, '<', $list) or die "Cannot open $list $!" ;
my @filelist = <$filelist_fh>;

foreach my $filelist (@filelist)
{
chomp $filelist ;
print "$filelist \n" ;

copy ("$filelist", "$ndirname1") or die "Cannot copy $filelist $! " ;
}

# while (my $line = <$filelist_fh>)
# {
# chomp $line ;
# print "$line \n" ;
# copy ($line, $ndirname1) or die "Cannot copy $line $! " ;
# }

exit ;

Code
 
And this is the array list :

C:/LogFiles-S/Tell_flow_log.2017-10-01.1.log
C:/LogFiles-S/Tell_flow_log.2017-10-01.2.log
C:/LogFiles-S/Tell_flow_log.2017-10-01.3.log
C:/LogFiles-S/Tell_flow_log.2017-10-01.4.log
C:/LogFiles-S/Tell_flow_log.2017-10-01.5.log
C:/LogFiles-S/Tell_flow_log.2017-10-02.1.log
C:/LogFiles-S/Tell_flow_log.2017-10-02.2.log
C:/LogFiles-S/Tell_flow_log.2017-10-02.3.log
C:/LogFiles-S/Tell_flow_log.2017-10-02.4.log
C:/LogFiles-S/Tell_flow_log.2017-10-02.5.log

Code



      
    


Chris Charley
User

Nov 19, 2017, 11:36 AM

Post #2 of 10 (2141 views)
Re: [tester_V] failing to copy files [In reply to] Can't Post

Hello,

This example should show you what you need.


Code
#!/usr/bin/perl 
use strict;
use warnings;
use File::Basename 'fileparse';

open my $fh, '<', \<<EOF;
C:/LogFiles-S/Tell_flow_log.2017-10-01.1.log
C:/LogFiles-S/Tell_flow_log.2017-10-01.2.log
C:/LogFiles-S/Tell_flow_log.2017-10-01.3.log
C:/LogFiles-S/Tell_flow_log.2017-10-01.4.log
C:/LogFiles-S/Tell_flow_log.2017-10-01.5.log
C:/LogFiles-S/Tell_flow_log.2017-10-02.1.log
C:/LogFiles-S/Tell_flow_log.2017-10-02.2.log
C:/LogFiles-S/Tell_flow_log.2017-10-02.3.log
C:/LogFiles-S/Tell_flow_log.2017-10-02.4.log
C:/LogFiles-S/Tell_flow_log.2017-10-02.5.log
EOF

my $ndirname1 = "C:/Sixshot_logs/Flow/";

while (my $line = <$fh>) {
chomp $line;
my $filename = fileparse($line);
print "$ndirname1$filename\n";
}


Output:

Code
 
C:\Old_Data\perlp>perl test2.pl
C:/Sixshot_logs/Flow/Tell_flow_log.2017-10-01.1.log
C:/Sixshot_logs/Flow/Tell_flow_log.2017-10-01.2.log
C:/Sixshot_logs/Flow/Tell_flow_log.2017-10-01.3.log
C:/Sixshot_logs/Flow/Tell_flow_log.2017-10-01.4.log
C:/Sixshot_logs/Flow/Tell_flow_log.2017-10-01.5.log
C:/Sixshot_logs/Flow/Tell_flow_log.2017-10-02.1.log
C:/Sixshot_logs/Flow/Tell_flow_log.2017-10-02.2.log
C:/Sixshot_logs/Flow/Tell_flow_log.2017-10-02.3.log
C:/Sixshot_logs/Flow/Tell_flow_log.2017-10-02.4.log
C:/Sixshot_logs/Flow/Tell_flow_log.2017-10-02.5.log



(This post was edited by Chris Charley on Nov 19, 2017, 3:49 PM)


tester_V
Novice

Nov 19, 2017, 12:34 PM

Post #3 of 10 (2139 views)
Re: [Chris Charley] failing to copy files [In reply to] Can't Post

Hi Chris!
Thank you for the code!

I'm not having problem reading from my array list,
I'm having problem copying files to a new directory.

I really appreciate your help!!!!!!!!!!!!!Smile


tester_V
Novice

Nov 19, 2017, 12:42 PM

Post #4 of 10 (2137 views)
Re: [tester_V] failing to copy files [In reply to] Can't Post

I just tried the code you sent me.
This part is not working:

open my $fh, '<', \<<EOF;

Thank you.


BillKSmith
Veteran

Nov 19, 2017, 1:15 PM

Post #5 of 10 (2127 views)
Re: [tester_V] failing to copy files [In reply to] Can't Post

This structure is called a 'here doc'. Refer to the operator <<EOF in perldoc. You are probably using old version of perl which requires the quote syntax.

Code
open my $fh, '<', \<<'EOF';

Good Luck,
Bill


Chris Charley
User

Nov 19, 2017, 1:27 PM

Post #6 of 10 (2127 views)
Re: [tester_V] failing to copy files [In reply to] Can't Post

open my $fh, '<', \<<EOF; reads the 'in-memory' file delimited by the EOF tokens at the top and bottom. I used this construct just to create the in-memory file instead of creating the file separately.

This feature became possible with perl version 5.08, I believe. If you're having problems, just open the file the way you do in your code - open (my $filelist_fh, '<', $list) or die "Cannot open $list $!"

My code just shows how to get the filename using 'fileparse' from the File::Basename module.

I just printed the new path/filename to illustrate how it works. You would use your copy here instead.

copy ($line, "$ndirname1$filename") or die "Cannot copy $line $!";


Laurent_R
Veteran / Moderator

Nov 19, 2017, 1:50 PM

Post #7 of 10 (2120 views)
Re: [tester_V] failing to copy files [In reply to] Can't Post

For copying a file, you need to use the File::Copy module. It is a core module, so it should be in any Perl install.

See https://perldoc.perl.org/File/Copy.html for documentation.


tester_V
Novice

Nov 19, 2017, 2:35 PM

Post #8 of 10 (2119 views)
Re: [Laurent_R] failing to copy files [In reply to] Can't Post

I do use File::copy module;
it does not want to copy files somehow.
This is the code I'm using:



#!/usr/local/bin/perl
use strict ;
use warnings ;
use File::Copy qw(move);
use File::Copy qw(copy);
use File::Basename 'fileparse';


my $ndirname1 = "C:/Sixshot_logs/Flow/";
mkdir $ndirname1 ;

my $list = "C:/Sixshot_logs/Flow-Sorted.txt" ;
open (my $filelist_fh, '<', $list) or die "Cannot open $list $!" ;
my @filelist = <$filelist_fh> ;

foreach my $filelist (@filelist)
{
chomp $filelist ;
print "$filelist \n" ;

copy ("$filelist", "$ndirname1") or die "Cannot copy $filelist $! " ;
}
#
# while (my $line = <$filelist_fh>)
# {
# chomp $line ;
# print "$line \n" ;
# copy ($line, $ndirname1) or die "Cannot copy $line $! " ;
# }

Code



      
    


Chris Charley
User

Nov 19, 2017, 3:42 PM

Post #9 of 10 (2112 views)
Re: [tester_V] failing to copy files [In reply to] Can't Post

My example should be all you need to do your task. I printed the new path/filename but all you need to do is copy instead of print. Here is the documentation for File::Basename (which is part of the core installation).

See how I used fileparse to get the filename (absent the path) from the original path/filename and then made the new path/filename. This is illustrated in the printout I provided.


tester_V
Novice

Nov 19, 2017, 4:28 PM

Post #10 of 10 (2106 views)
Re: [tester_V] failing to copy files [In reply to] Can't Post

I found why I had a problem copying.
My array list file did not have the the path to the directory for the files to copy.

Thank you all for help!
really appreciate your help.
You guys great!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives