The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
copy a directory plus contents??
Discuss copy a directory plus contents?? in the Perl Programming forum on Dev Shed. copy a directory plus contents?? Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 15th, 2000, 12:05 PM
|
|
Contributing User
|
|
Join Date: May 2000
Posts: 73
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
how do i copy a directory and its contents from a perl/cgi script?
|

August 15th, 2000, 02:41 PM
|
|
Guest
|
|
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.
|

August 15th, 2000, 06:36 PM
|
|
Contributing User
|
|
Join Date: Aug 2000
Location: Indiana
Posts: 614
  
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).
|

August 15th, 2000, 10:04 PM
|
|
Guest
|
|
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
|

August 15th, 2000, 11:30 PM
|
|
Guest
|
|
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.
|

August 16th, 2000, 12:57 AM
|
|
Contributing User
|
|
Join Date: Aug 2000
Location: Indiana
Posts: 614
  
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.
|

August 16th, 2000, 04:01 AM
|
|
Contributing User
|
|
Join Date: Aug 2000
Posts: 81
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).]
|

August 16th, 2000, 08:39 AM
|
|
Contributing User
|
|
Join Date: May 2000
Posts: 73
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
` ` did the trick
Thank yo'all !
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|