Webmaster Forum Rules | Posting Guide | Contact Us | Testimonials | Contributing Geek Program | Advertise on Geek/Talk
Welcome to the GeekTalk Webmaster Discussion Forums from GeekVillage.com

Click Here To Register. It's Free!

Go Back   geek/talk: Signature-free discourse for serious web publishers > YOUR CREATION: Building & Maintaining A Web Site > Web Development - Programming / Coding
User Name
Register FAQ Calendar Search Today's Posts Mark Forums Read

Thread Tools Display Modes
Old 01-28-2002, 05:31 AM   #1
Registered User
Join Date: Dec 2000
Location: UK (South West)
Posts: 104
Question Copy and rename command in Perl ?


I need to copy and rename a file using a Perl script. I'm reasonably competent with Perl but can't find the necessary command in any of my usual references/urls.

I need something like:

copyfile "/cgi-files/oldfile.txt", "/cgi-files/copyofoldfile.txt";

Could someone advise the correct command and syntax.

Best regards

TSSR is offline   Reply With Quote
Old 01-28-2002, 11:58 AM   #2
Registered User
AK's Avatar
Join Date: Jan 2000
Location: Lkpg, SE
Posts: 562

From the Perl Cookbook:

You need to copy a file, but Perl has no built-in copy command.


Use the copy function from the standard File::Copy module:

use File::Copy;
copy($oldfile, $newfile);

You can do it by hand:

open(IN, "< $oldfile") or die "can't open $oldfile: $!";
open(OUT, "> $newfile") or die "can't open $newfile: $!";

$blksize = (stat IN)[11] || 16384;
# preferred block size?
while ($len = sysread IN, $buf, $blksize) {
if (!defined $len) {
next if $! =~ /^Interrupted/;
# ^Z and fg
die "System read error: $!\n";
$offset = 0;
while ($len) {
# Handle partial writes.
defined($written = syswrite OUT, $buf, $len, $offset)
or die "System write error: $!\n";
$len -= $written;
$offset += $written;


Or you can call your system's copy program:

system("cp $oldfile $newfile"); # unix
system("copy $oldfile $newfile"); # dos, vms

Last edited by AK; 01-28-2002 at 12:11 PM.
AK is offline   Reply With Quote
Old 01-28-2002, 08:07 PM   #3
I am a Contributing Geek. Are You?
Join Date: Aug 2000
Location: CA USA
Posts: 484

You can always do it the old fashioned way and tell it to do it from the command line:

`cp cgi-files/oldfiles.txt cgi-files/copyoldfile.txt`;

That will make the new file have the permissions of whatever the web server is at (probably user nobody or something), though....if you can install the perl module, do that instead. Just confusing you/giving you more options =)
ccole is offline   Reply With Quote
Old 01-29-2002, 04:46 AM   #4
Registered User
Join Date: Dec 2000
Location: UK (South West)
Posts: 104

Thanks guys



module does the job

TSSR is offline   Reply With Quote


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Please support our advertisers. They ensure our survival.

All times are GMT -5. The time now is 08:19 AM.

GeekVillage.com is copyright © 1998-2015 Curiosity Cave - Science gifts for clever kids. All rights reserved.
Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.