On Sun, 23 May 2010 20:09:52 -0500 Chap Harrison <c...@pobox.com> wrote:
> Hi, > > I can't believe I'm having such a hard time with this. I just want a > platform-independent way of copying a directory 'A' and its contents > into another directory 'B' (as a subdirectory of B, named A). Not > crossing filesystems or devices. > Various online sources have said you copy directories the same way > you copy files; others have recommended File::Copy as a > platform-independent copy/move command; and the File::Copy docs > contain the following statement: "If the destination already exists > and is a directory, and the source is not a directory, then the > source file will be renamed into the directory specified by the > destination." > > So what if the source *IS* a directory?? > > This is what I've tried. I've also tried 'move' rather than 'copy', > and even appending '/' to the ends of the directory names. Thanks > for a clue! The below is totally untested, but might give you some ideas. To make a directory unless ( -d "B" ){mkdir "B"} Then read all the files in A opendir( my $DB_DIR, "A" ); my @files = readdir($DB_DIR); close $DB_DIR; Then copy them across; foreach my $file(@files){copy "$file", "B/$file"}; The variable for the OS is "$^O". You need to use this the then set up your paths etc. However you might want to look at http://search.cpan.org/~dagolden/Perl-OSType-0.005/lib/Perl/OSType.pm Owen > > #!/usr/bin/perl > > use warnings; > use strict; > use File::Copy; > use File::Touch; > use feature ":5.10"; > > my $f = "foo-dir"; > my $b = "bar-dir"; > my $fsub = "$f/subfoo-dir"; > my $bsub = "$b/subbar-dir"; > > mkdir $f; > mkdir $b; > mkdir $fsub; > mkdir $bsub; > > my $fsubsub_file = "$fsub/subsubfoo-file.txt"; > touch $fsubsub_file; > > my $bsubsub_file = "$bsub/subsubbar-file.txt"; > touch $bsubsub_file; > > # foo-dir/ > # subfoo-dir/ ($fsub) > # subsubfoo-file.txt > # > # bar-dir/ > # subbar-dir/ ($bsub) > # subsubbar-file.txt > # > # > # Now - how can I copy subbar-dir/ and its contents into subfoo-dir/ > as a subdirectory ?? # > # I.E. I want this: > # > # foo-dir/ > # subfoo-dir/ > # subsubfoo-file.txt > # subbar-dir/ > # subsubbar-file.txt > # > > say "copy $bsub, $fsub"; > copy $bsub, $fsub or say "Copy directory->directory didn't work: > $!"; # I've also tried move. Neither works. > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/