|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
how do i copy a directory and its contents from a perl/cgi script?
|
|
#2
|
|||
|
|||
|
#!/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. |
|
#3
|
|||
|
|||
|
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). |
|
#4
|
|||
|
|||
|
wouldn't you want
`cp -Rf $from $to`; instead? I've heard that not combining options can cause problems |
|
#5
|
|||
|
|||
|
>>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. |
|
#6
|
|||
|
|||
|
<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. |
|
#7
|
|||
|
|||
|
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).] |
|
#8
|
|||
|
|||
|
` ` did the trick
Thank yo'all ! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > copy a directory plus contents?? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|