Debbie Cooper wrote:
I need to combine directories as follows:

Source directory structure:

DirA
        SubDirAA
                More directories
        SubDirAB
                More directories
        SubDirAC
                More directories
DirB
        SubDirAA
                More directories
        SubDirAB
                More directories
        SubDirBC
                More directories


I want to copy these directories so that the target directory looks like this:

Target directory structure

SubDirAA
        combined directories
SubDirAB
        combined directories
SubDirAC
        combined directories
SubDirBC
        combined directories

So the sub directories in DirA will be combined with like-named sub
directories in DirB and they will move up a level in the hierarchy.

Untested:

use warnings;
use strict;
use File::Find;
use File::Copy;
use File::Path;
use File::Spec;

my @dirs = qw( DirA DirB );

for my $dir ( @dirs ) {
    find( sub {
        my @paths = File::Spec->splitdir( $File::Find::dir );
        for my $i ( 0 .. $#paths ) {
            if ( $path[$i] eq $dir ) {
                splice @paths, $i, 1;
                last;
                }
            }
        my $new_dir = File::Spec->catdir( @paths );
        mkpath( $new_dir ) unless -d $newdir;
        if ( -e "$newdir/$_" ) {
            warn "$newdir/$_ already exists, cannot copy!\n";
            }
        else {
            copy( $_, "$newdir/$_" );
            }
        }, $dir );
    }


I have the following script for combining txt files in a directory but I
don't know the Perl syntax well enough to apply it to combining directories
(if I can modify this script for that purpose).  I've done an extensive web
search but haven't found much that helps.  Can someone point me in the right
direction?

#!perl
use warnings;

for my $f ( map "lv$_.txt", 1..960)
{
        for ( grep -f, map $_.$f, qw( a/ b/ c/ d/ e/))
        {
                $f2 = $_;

                open STDIN, $_;
                open STDOUT, ">>$f";

                while( <STDIN> ) {
                        unless($_ =~ /username/ ) {
                                print STDOUT "$_";
                        }elsif ($f2 =~ /a/ ) {
                                print STDOUT "$_";
                        }
                }
        }
}

This is a great script

It doesn't look THAT great. You try to open the files without verifying that they have in fact opened correctly. You use STDIN and STDOUT instead of defining your own filehandles. You unnecessarily put scalars in quotes.


that I believe I got from one of you listers.

Was that me? I believe I posted something like:

#!/usr/bin/perl
use warnings;
use strict;

for my $f ( map "LV$_.txt", 1 .. 960 ) {
    for( grep -f, map "$_/$f", qw( a b c d ) ) {
        open IN, $_;
        open OUT, ">>$f";
        while ( <IN> ) {
            next if $. == 1 and -s OUT;
            print OUT;
        }
        close IN;
    }
}

If that was me, I apologise for the bad example!


John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to