Hi, all,

I need to process one folder structure into a new folder structure, e.g.
replace the word "dog" with "cat" recursively down through a folder, writing
the updated files into a new structure:

% ls
myfolder1
% dog2cat myfolder1
dog2cat:  myfolder1 --> myfolder1.cat
% ls
myfolder1    myfolder1.cat

Where myfolder1.cat has all the contents of myfolder1, except that all
occurrences of "dog" in any of the files have been replaced with "cat".

My problem has been generating the new folder structure at the cwd.  Has
anyone seen this done before?  I know mostly how to use File::Find, but it
gets sticky because it doesn't provide a relative pathname for each new file
to the original path supplied.  There's got to be an easier way than what
I'm doing (see below).  It's main problem right now is that it doesn't work
if the user happens to supply a starting folder like
"myfolder1/mysubfolder1".

Any tips would be much appreciated.

- Bryan

**************************************


[stuff cut out]

    # get temporary destination (to ease replace process using File::Find)
and create
    $tdest = cwd() . "/temp.qns/";
    mkdir($tdest,0777) or die "qns:  Error 503:  Cannot create directory
$tdest: $!";

    # grind through the original folder structure, build structure and do
replacements
    find(\&process_file, ($source));

    # move items to their final destination
    rename($tdest.$source,$fdest) or die "qns:  Error 504:  Can't move
$tdest$source to $fdest: $!";
    rmdir("temp.qns") or die "qns:  Error 505:  Couldn't remove temp
directory temp.qns: $!";

[more stuff cut out]

# ---  subroutines  -------------------------------------------------------

sub process_file {

$thisdest = $tdest . $File::Find::name;

    # if the file is a directory, create corresponding structure in new
folder
    if (-d) { mkdir($thisdest,0777) or die "qns:  Error 601:  Cannot create
directory $thisdest: $!"; }

    # otherwise, process
    else {

        # read in file
        open(FILE, $_) or die "qns:  Error 602:  Couldn't open
$File::Find::name: $!\n";
        undef $/;
        $_ = <FILE>;
        close(FILE);
        
        # make replacements
        s/$s1/qq(qq($s2))/gmee;
        
        # write out file to new location
        open(OFILE, "> $thisdest") or die "qns:  Error 603:  Couldn't open
$thisdest: $!\n";
            print OFILE;
        close(OFILE);
    }
    
}

Reply via email to