FAQ 5.5 How can I copy a file?

FAQ 5.5 How can I copy a file?

am 21.12.2007 15:03:03 von PerlFAQ Server

This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

------------------------------------------------------------ --------

5.5: How can I copy a file?


(contributed by brian d foy)

Use the File::Copy module. It comes with Perl and can do a true copy
across file systems, and it does its magic in a portable fashion.

use File::Copy;

copy( $original, $new_copy ) or die "Copy failed: $!";

If you can't use File::Copy, you'll have to do the work yourself: open
the original file, open the destination file, then print to the
destination file as you read the original.



------------------------------------------------------------ --------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Re: FAQ 5.5 How can I copy a file?

am 22.12.2007 10:24:15 von Adam

On Dec 21, 6:03 am, PerlFAQ Server wrote:
> This is an excerpt from the latest version perlfaq5.pod, which
> comes with the standard Perl distribution. These postings aim to
> reduce the number of repeated questions as well as allow the community
> to review and update the answers. The latest version of the complete
> perlfaq is athttp://faq.perl.org.
>
> ------------------------------------------------------------ --------
>
> 5.5: How can I copy a file?
>
> (contributed by brian d foy)
>
> Use the File::Copy module. It comes with Perl and can do a true copy
> across file systems, and it does its magic in a portable fashion.
>
> use File::Copy;
>
> copy( $original, $new_copy ) or die "Copy failed: $!";
>
> If you can't use File::Copy, you'll have to do the work yourself: open
> the original file, open the destination file, then print to the
> destination file as you read the original.
>
> ------------------------------------------------------------ --------
>
> The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
> are not necessarily experts in every domain where Perl might show up,
> so please include as much information as possible and relevant in any
> corrections. The perlfaq-workers also don't have access to every
> operating system or platform, so please include relevant details for
> corrections to examples that do not work on particular platforms.
> Working code is greatly appreciated.
>
> If you'd like to help maintain the perlfaq, see the details in
> perlfaq.pod.

could also do a quick system command

`cp $orig $new`;

Re: FAQ 5.5 How can I copy a file?

am 22.12.2007 11:30:11 von Michele Dondi

On Sat, 22 Dec 2007 01:24:15 -0800 (PST), Adam
wrote:

>could also do a quick system command
>
>`cp $orig $new`;

Just like with quite about anything else one may want to do. But if
every faq entry did contain a remark of this kind for all of them,
then they would amount pretty much to a shell manual as a whole. And
you know, the times Perl was posted to comp.sources.unix are long
gone. Your suggestions suffers from the well known, usual portability
issues. Since copying a file is a portable concept, this may matter.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: FAQ 5.5 How can I copy a file?

am 22.12.2007 17:53:51 von merlyn

>>>>> "Michele" == Michele Dondi writes:

>> could also do a quick system command
>>
>> `cp $orig $new`;

Michele> Just like with quite about anything else one may want to do. But if
Michele> every faq entry did contain a remark of this kind for all of them,
Michele> then they would amount pretty much to a shell manual as a whole. And
Michele> you know, the times Perl was posted to comp.sources.unix are long
Michele> gone. Your suggestions suffers from the well known, usual portability
Michele> issues. Since copying a file is a portable concept, this may matter.

Additionally, you've got the broken idiom of using backquotes in a
void context, and not properly accounting for shell metacharacters.
I mean, even if you *wanted* to use cp, you should do this:

system "cp", $orig, $new;

*Sigh*. Kids these days. :)

print "Just another Perl hacker,"; # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Re: FAQ 5.5 How can I copy a file?

am 09.01.2008 00:19:09 von kwalike57

On Dec 22 2007, 8:53=A0am, mer...@stonehenge.com (Randal L. Schwartz)
wrote:
> >>>>> "Michele" == Michele Dondi writes:
> >> could also do a quick system command
>
> >> `cp $orig $new`;
>
> Michele> Just like with quite about anything else one may want to do. But =
if
> Michele> every faq entry did contain a remark of this kind for all of them=
,
> Michele> then they would amount pretty much to a shell manual as a whole. =
And
> Michele> you know, the times Perl was posted to comp.sources.unix are long=

> Michele> gone. Your suggestions suffers from the well known, usual portabi=
lity
> Michele> issues. Since copying a file is a portable concept, this may matt=
er.
>
> Additionally, you've got the broken idiom of using backquotes in a
> void context, and not properly accounting for shell metacharacters.
> I mean, even if you *wanted* to use cp, you should do this:
>
> =A0 system "cp", $orig, $new;
>
> *Sigh*. =A0Kids these days. :)
>
> print "Just another Perl hacker,"; # the original
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 009=
5
>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl traini=
ng!

Hello.

I have a question along these lines. I have a perl script that in a
nutshell, reads a directory and then looks for a specific file naming
convention which is matched up by using a regExp that loads a variable
$FILE. Once it finds this it Connect:Directs (copies file) the file
to a remote node. After this is done I send an email notification
that the file transfer completes. My problem is that when this whole
process completes, I want to rename the $FILE to a file.done format.
I do this with the rename function normally but saying rename ($FILE,
$FILEDONE). This is not working. I can tell the script to print out
the value of $FILE and $FILEDONE and both values are correctly
indicated. For example if $FILE is FILEa.txt then $FILEDONE would be
FILEa.txt.done.

So my question is, why won't my file rename properly. My script exits
with an error indicating cannot rename $FILE.

In light of this I decided to try to copy $FILE and then also tried to
move $FILE. Neither work. This script runs on a Windows box and so I
tried to manually execute the copy and move commands from the command
line and both work fine. I can rename, copy and move the file with no
problem. I am using File::Copy in my script. I have tried using the
system command to execute the copy and moves and I have tried to
manually type in the copy and moves as well in the script. I have
only tried to do this with the variable values and have not tried
executing these commands with a qualified path to the file name.

Any ideas? This seems like it should be the easiest part of the whole
process and it has taken up a bunch of time trying to get this last
element to work.

Thanks for your help!!

Karin Walike

Re: FAQ 5.5 How can I copy a file?

am 09.01.2008 01:30:33 von Jim Gibson

In article
<2a946010-5182-4f17-8ceb-50b9160fb8e7@j78g2000hsd.googlegroups.com>,
kwalike57 wrote:


> Hello.
>
> I have a question along these lines.

Note that it is best to start a new thread if you have a new problem
(which I now notice you have already done. Grr!).

[problem renaming file in Perl under Windows snipped]

Write a small test script that renames a single test file. Check the
return value from rename and, if it fails, print the special variable
$!. Something like this (untested):


use strict;
my $file = 'test.txt';
my $newfile = "$file.done";
rename($file,$newfile) or die($!);


If that works, incorporate it into your full program. If it doesn't
work, and you can't figure out why not, post the program and the actual
results here.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com