in reply to Writing one File from Various Files in a Directory

If I understand correctly, you want to loop through all the files in one directory, pull the text out of those files and stick the text into another file? I did a similar thing on a Windows box and found the following code worked for me:
my $dir = "location of your file directory"; my $filename = "the location & name of your new file"; open (LOGFILE, ">$filename") or die "Can't create logfile: $!"; opendir (DATADIR, $dir) or die "Can't open $dir: $!"; while( defined ($file = readdir DATADIR) ) { next if $file =~ /^\.\.?$/; open (INFILE, "$dir\\$file") or die "Can't open $dir: $!"; print LOGFILE "$file|"; $/ = ''; while (<INFILE>) { @lines = $_; } foreach (@lines) { print LOGFILE $_; } } close (INFILE);
It's been awhile since I've used this snippet, but it might get you started. I did take the data in the LOGFILE and import it into an Access database, so it will probably go into Excel without too much trouble.

Also, I'm pretty new with Perl, so more than likely there's a better, shorter way to do it that a more experienced Monk could share. HTH,
WB

Replies are listed 'Best First'.
Re: Re: Writing one File from Various Files in a Directory
by CountZero (Bishop) on Jun 21, 2003 at 06:53 UTC

    Just wondering why you would set $/ to the null string?

    First I would suggest doing so in a local-context (although it doesn't hurt in such a small script as yours) so it gets automatically "reset" when you leave the enclosing scope.

    By setting $/ to the null string, the input record separator becomes one or more blank lines (multiple blank lines gets collapsed into one).

    If you just want to slurp the whole of the file at once (without collapsing multiple blank lines into one as a side effect), do a

    undef $/;
    and then you can drop the whole
    while (<INFILE>) {@lines = $_;} foreach (@lines) {print LOGFILE $_;}
    loop and just do a
    $line=<INFILE>; print LOGFILE, $line;

    Staying closer to your script

    while (my $line=<INFILE>){print LOGFILE,$line;}
    is somewhat more elegant, IMHO.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Thanks for the input on this point, Count. I originally used this script to get name/address information off of cover sheets used for mailing reports. I needed to slurp the information on each page into one line. However, I think the question in this case is going to require your approach. ++For the explanation.