Copying more than one file using File::Copy

Good day=3B

I apologize in advance if this is a very stupid question=85

I=27m trying to concatenate two files and have the results written to a =

third file=2E =

Maybe I=27m missing something obvious=2E=2E=2E but in Perldoc for File=3A=
=3ACopy I =

read=3A =22The copy function takes two parameters=3A a file to copy from =
and =

a file to copy to=2E=22

What can I do if I have a second file I want to copy from=3F (i=2Ee=2E Li=
ke =

DOS copy command=3A c=3A=5C file1=2Etxt + file2=2Etxt file3=2Etxt will co=
py the =

contents of file1=2Etxt =26 file2=2Etxt and put the combined contents int=
o =

file3=2Etxt)

Thank you all for your time=2E
Carl


0
crogers3
8/8/2002 9:00:45 PM
📁 perl.beginners
📃 29378 articles.
⭐ 3 followers.

💬 3 Replies
👁️‍🗨️ 899 Views

--=_8ED2EC6C.FB9AEF9F
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Also being a Perl n00b myself, my suggestion is that you need to do some =
more work. What about something like:

open FILE, "< filename" or die("blahblahblah");
open FILE2, "< filename2" or die("more blahblahblah");
open FILE3, "> filename3" or die ("still blahblahblah");

my $foo =3D join ("", <FILE>);
my $bar =3D join ("", <FILE2>);

my $newbar =3D $foo . $bar;

print <FILE3> $newbar;

I'm sure I'm re-inventing the wheel here, and I realize I am not using =
File::Copy at all like you desired, and this is probably a pretty cheesy =
way to do this, but it is something to ponder, and I'm sure some of the =
more knowledgeable Perl-keteers can give better suggestions.




John Pitchko
Data Services
Saskatchewan Government Insurance

>>> <crogers3@gmu.edu> 08/08/02 03:00pm >>>
Good day;

I apologize in advance if this is a very stupid question*

I'm trying to concatenate two files and have the results written to a=20
third file.=20
Maybe I'm missing something obvious... but in Perldoc for File::Copy I=20
read: "The copy function takes two parameters: a file to copy from and=20
a file to copy to."

What can I do if I have a second file I want to copy from? (i.e. Like=20
DOS copy command: c:\ file1.txt + file2.txt file3.txt will copy the=20
contents of file1.txt & file2.txt and put the combined contents into=20
file3.txt)

Thank you all for your time.
Carl



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

--=_8ED2EC6C.FB9AEF9F--
0
jpitchko
8/8/2002 9:40:34 PM
crogers3@gmu.edu wrote:
> 
> Good day;

Hello,

> I apologize in advance if this is a very stupid question?
> 
> I'm trying to concatenate two files and have the results written to a
> third file.
> Maybe I'm missing something obvious... but in Perldoc for File::Copy I
> read: "The copy function takes two parameters: a file to copy from and
> a file to copy to."
> 
> What can I do if I have a second file I want to copy from? (i.e. Like
> DOS copy command: c:\ file1.txt + file2.txt file3.txt will copy the
> contents of file1.txt & file2.txt and put the combined contents into
> file3.txt)


One possible solution:

use File::Copy;

my @from = qw/file1 file2 file3/;
my $to = 'newfile';

open NEW, ">$to" or die "Cannot open $to: $!";
binmode NEW;

for my $file ( @from ) {
    copy( $file, \*NEW ) or die "Cannot copy $file: $!";
    }

__END__



John
-- 
use Perl;
program
fulfillment
0
krahnj
8/8/2002 10:14:40 PM
On Thursday, August 8, 2002, at 02:00 , <crogers3@gmu.edu> wrote:
[..]
> Maybe I'm missing something obvious... but in Perldoc for File::Copy I
> read: "The copy function takes two parameters: a file to copy from and
> a file to copy to."

for fun you might want to do the

	perldoc -m File::Copy

and see how the sausage is made...

since it is basically about 'read the input file' and
write it out to the output file - using 'syscopy' - which
is implemented in the Win32 space with

	Win32::CopyFile(@_, 1);

which you may wish to look at...

alternatively let's move along....

what you want is something that does

	dump_across file1 file2 file3 outputfile

that would be:
	
	#!/usr/bin/perl -w
	use strict;

	my $outFile = pop @ARGV;

	open(OUT, "> $outFile") or die "unable to open output file $outFile 
:$!\n";
	for my $file (@ARGV) {

		open(IN, "$file") or die "unable to open input file $file :$!\n";
		print $OUT $_ while(<IN>) ;
		close(IN);
	}
	close(OUT);

you wil of course need to tweek that for the Win32 environment,
since you will probably want to deal with how to make sure that
it reads and writes appropriately - but I think you get the main drift.

[..]

ciao
drieux

---

0
drieux
8/8/2002 10:18:08 PM
Reply: