star7 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I wrote a long script in perl. Now i have a problem. A part of my code:
sub textsformat { # variables: initialization ... ... ... my $text =$_[0]; open (MYFILE, "<$text") || die "the file $text is not readable: $!" ; open (OUTPUT, ">$text.txt") || die "can not open $text\.txt with the permission write: $!"; LINE: while(<MYFILE>) { ..... # Algo for formatting a textfile }; close(MYFILE); close(OUTPUT); }; sub dir_explorer { my $entry = $_ ; if (-d $entry) { ... # ignore . and .. opendir(DIR, $entry) or die "Can not open $entry: $!"; open(FILE, ">>$entry.tab") or die "Can not open $entry with the p +ermission write: $!"; while( defined (my $file = readdir DIR) ) { if (-f "$entry/$file") { textsformat("$entry/$file"); # .... }; }; close(FILE); closedir(DIR); }; }; find(\&dir_explorer, "/home/dub/S/programming/perl/dp");
the subroutine dir_explorer goes through the indicated directory recursively. For each text file in a directory the subroutine textsformat is invoked. Intention: successively: for each textfile of a directory the subroutine textsformat is invoked ,following the new formatted text file has to be inserted into the textfile (see open(FILE, ">>$entry.tab"), which contains all formatted textfiles of a directory.How can I solve the problem as best. Also I want to assign the name of the visited directory as parameter to the subroutine textsformat. Please indicates also the suitable code. I hope you understand my problem.

Thank you

best wishes
star7

edited: Sun Jun 1 15:55:03 2003 by jeffa - title change (was: good solution ...)

Replies are listed 'Best First'.
Re: Reformatting while traversing directories
by allolex (Curate) on May 31, 2003 at 21:57 UTC

    Hi, I didn't completely understand what you are asking, but it looks like what you are trying to do can be done by File::Find. Have a look at its documentation on CPAN and see if that helps.

    Update: I see you've been given this advice before in Re: visit directories and more....

    --
    Allolex

      @allolex
      Hi, the code is complete (on my computer, not here). My code use File::Find without problems. My problem is: the subroutine textsformat formats a textfile(saved in, see open (OUTPUT, ">$text.txt"). How can I append this file ($text.txt) to the file (saved in, see open(FILE, ">>$entry.tab") ? I hope you understand my question. Also I hope you can help me.
      star7

        Ah, then you'll want to have a look at Graff's solution in this thread.

        --
        Allolex

Re: Reformatting while traversing directories
by graff (Chancellor) on Jun 01, 2003 at 05:59 UTC
    If I understand you right, for each directory that you go into, you want to create modified versions of all the files inside that directory, and you also want to create a single file, next to that same directory (not inside it), that contains the concatenation of all the modified file contents.

    There are many ways to go about this, but the method that would probably involve the least amount of code revision would be for the "dir_explorer" function to go something like this:

    sub dir_explorer { my $entry = $_ ; if (-d $entry) { ... # ignore . and .. opendir(DIR, $entry) or die "Can not open $entry: $!"; # don't bother with "open(FILE, ">>$entry.tab") while ( defined( my $file = readdir DIR ) { if ( -f "$entry/$file" ) { textsformat( "$entry/$file" ); } # now "$entry/$file.txt" should exist, so: system "cat $entry/$file.txt >> $entry.tab"; } closedir DIR: } }
      @graff
      Thank you.
      star7