Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 15th, 2000, 12:05 PM
des des is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Posts: 73 des User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
how do i copy a directory and its contents from a perl/cgi script?

Reply With Quote
  #2  
Old August 15th, 2000, 02:41 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
#!/usr/local/bin/perl

$from = "/full/path/to/foo/bar";

$to = "/full/path/to/another_dir/bar";

print "Content-type: text/htmlnn";
`cp -R $from $to`;
print "Done";

Please note, if Apache is run as nobody:nogroup, you need to chmod "another_dir" to 757 in advance. If it's run as, let say "www" and the "bar" is also belong to "www" group, then chmod 775 should be fine. If neither ones works, chmod it to 777.

Reply With Quote
  #3  
Old August 15th, 2000, 06:36 PM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 14
Better make this:
`cp -R $from $to`;
this:
`cp -R -f $from $to`;

This will over right the file if it already exists (for backing up on a regular basis).

Reply With Quote
  #4  
Old August 15th, 2000, 10:04 PM
RyanP
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
wouldn't you want

`cp -Rf $from $to`;

instead? I've heard that not combining options can cause problems

Reply With Quote
  #5  
Old August 15th, 2000, 11:30 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>This will over right the file if it already exists

In my example, /full/path/to/another_dir/bar, the "bar" directory doesn't exist. There is no point to use the -f flag.

Reply With Quote
  #6  
Old August 16th, 2000, 12:57 AM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 14
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>wouldn't you want
`cp -Rf $from $to`;

instead? I've heard that not combining options can cause problems
[/quote]
Yeah, could be. I just know that it doesn't mess up on my OS, so that's what I use.

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>In my example, /full/path/to/another_dir/bar, the "bar" directory doesn't exist. There is no point to use the -f flag.[/quote]
Not the 1st time the script is run. But if it's setup to run on say a crontab, then it'll need to over right the old files each time.

Reply With Quote
  #7  
Old August 16th, 2000, 04:01 AM
christucker2 christucker2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Posts: 81 christucker2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
A more portable approach would be to use the File::Copy and File::Find modules (with File::Path to make directories). The following is untested, but should work:

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
use File::Copy;
use File::Find;
use File::Path;

my $source = 'mysourcedir';
my $target = 'mytargetdir';

find( copy_files, $source );

# note: for the copy sub, $_ is set to the current file being tested, and we are chdir'd to the directory that file resides in.
sub copy_files {
my $targetdir = $File::Find::dir;
$targetdir =~ s/Q$source/Q$target/o;
mkpath( $targetdir ) if not -e $targetdir;
copy($_, "$target/$_") if -f;
}
[/code]

It would be a trivial task (and probably one that has already been done, though I haven't checked CPAN) to write a portable module that performs a recursive copy using this code. Currently, this performs a recursive copy. This behaviour could be changed by setting $File::Find:: prune = 1, thereby preventing the copy descending into subdirectories (apologies, had to put a space between Find:: and prune which shouldn't be there as the damn messaging system was ignoring the 'disable smilies' option when editing...)

[This message has been edited by christucker2 (edited August 16, 2000).]

Reply With Quote
  #8  
Old August 16th, 2000, 08:39 AM
des des is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Posts: 73 des User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
` ` did the trick


Thank yo'all !

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > copy a directory plus contents??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap