f



Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file.

I am trying to concatenate all files in a directory together into one
file and read the first 225 characters of each file into one file as
sort of a summary file.

This does cat all file together into one bigfile

my $directory = "c:\\myfiles";
my $bigfile =c:\\bigfile.txt";

opendir (DIR, $directory) or die $!;
@ARGV = readdir(DIR);

chdir( $directory ) or die $!; #Need to do this readdir has just
filename
@ARGV = grep( -f, @ARGV);

foreach (@ARGV) {
    local $/ ;
    open OUT,">$bigfile" or die $!;
    while( <> ){
        print OUT $_, "\n";
        }
    }
close OUT;
closedir(DIR)

Now this does read the first 225 characters from each file but I need
to put it into a single file, not to STDOUT and I can not get the two
to run together in the same script.

chdir( $directory ) or die $!;
@ARGV = grep( -f, @ARGV);
$files = shift(@ARGV);
while (<>) {
open(INFILE,"<$files") or die "Can't open: $!\n";
read(INFILE,$buff,225,0);
print "$buff\n";
}
close(INFILE);

Many thanks for any help..
0
4/18/2004 2:30:50 PM
comp.lang.perl.misc 33233 articles. 2 followers. brian (1246) is leader. Post Follow

5 Replies
1026 Views

Similar Articles

[PageSpeed] 23

Tony <tsciortino@hotmail.com> wrote:
> I am trying to concatenate all files in a directory together into one
> file and read the first 225 characters of each file into one file as
> sort of a summary file.
> 
> This does cat all file together into one bigfile


No it doesn't.

It stomps on $bigfile for each little file...


> my $directory = "c:\\myfiles";


If you use single quotes you won't have to remember to double backslashes:

   my $directory = 'c:\myfiles';

If you use sensible slashes, then you can use either kind of quotes:

   my $directory = "c:/myfiles";
or
   my $directory = 'c:/myfiles';



> my $bigfile =c:\\bigfile.txt";
              ^^
              ^^ where's the quote?


Please don't try and retype code.

Have you seen the Posting Guidelines that are posted here frequently?


> opendir (DIR, $directory) or die $!;
> @ARGV = readdir(DIR);
> 
> chdir( $directory ) or die $!; #Need to do this readdir has just
> filename
> @ARGV = grep( -f, @ARGV);
> 
> foreach (@ARGV) {
>     local $/ ;
>     open OUT,">$bigfile" or die $!;


$bigfile will contain ONLY the last file from @ARGV, which isn't
what you said it is doing.

So then, the code you have is not the code you've shown us.

We cannot help fix code that we cannot see...


> Now this does read the first 225 characters from each file but I need
> to put it into a single file, not to STDOUT


Then open() some other filehandle and print() to the other filehandle.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas
0
Tad
4/18/2004 7:20:58 PM
Tony wrote:
> 
> I am trying to concatenate all files in a directory together into one
> file and read the first 225 characters of each file into one file as
> sort of a summary file.
> 
> This does cat all file together into one bigfile
> 
> my $directory = "c:\\myfiles";
> my $bigfile =c:\\bigfile.txt";
> 
> opendir (DIR, $directory) or die $!;
> @ARGV = readdir(DIR);
> 
> chdir( $directory ) or die $!; #Need to do this readdir has just
> filename
> @ARGV = grep( -f, @ARGV);
> 
> foreach (@ARGV) {
>     local $/ ;
>     open OUT,">$bigfile" or die $!;
>     while( <> ){
>         print OUT $_, "\n";
>         }
>     }
> close OUT;
> closedir(DIR)
> 
> Now this does read the first 225 characters from each file but I need
> to put it into a single file, not to STDOUT and I can not get the two
> to run together in the same script.
> 
> chdir( $directory ) or die $!;
> @ARGV = grep( -f, @ARGV);
> $files = shift(@ARGV);
> while (<>) {
> open(INFILE,"<$files") or die "Can't open: $!\n";
> read(INFILE,$buff,225,0);
> print "$buff\n";
> }
> close(INFILE);


This should work (untested):

my $directory = 'c:/myfiles';
my $bigfile   = 'c:/bigfile.txt';
my $summary   = 'c:/summary.txt';

opendir DIR, $directory or die $!;
@ARGV = map "$directory/$_", grep -f, readdir DIR;

open OUT, '>', $bigfile or die "Cannot open $bigfile: $!";
open SUM, '>', $summary or die "Cannot open $summary: $!";

$/ = \225;
while ( <> ) {
    print SUM if $. == 1;
    print OUT;
    close ARGV if eof;
    }



John
-- 
use Perl;
program
fulfillment
0
John
4/19/2004 1:50:19 AM
Tad McClellan <tadmc@augustmail.com> wrote in message news:<slrnc85l8q.1mn.tadmc@magna.augustmail.com>...
> Tony <tsciortino@hotmail.com> wrote:
> > I am trying to concatenate all files in a directory together into one
> > file and read the first 225 characters of each file into one file as
> > sort of a summary file.
> > 
> > This does cat all file together into one bigfile
> 
> 
> No it doesn't.
> 
> It stomps on $bigfile for each little file...
> 
> 
> > my $directory = "c:\\myfiles";
> 
> 
> If you use single quotes you won't have to remember to double backslashes:
> 
>    my $directory = 'c:\myfiles';
> 
> If you use sensible slashes, then you can use either kind of quotes:
> 
>    my $directory = "c:/myfiles";
> or
>    my $directory = 'c:/myfiles';
> 
> 
> 
> > my $bigfile =c:\\bigfile.txt";
>               ^^
>               ^^ where's the quote?
> 
> 
> Please don't try and retype code.
> 
> Have you seen the Posting Guidelines that are posted here frequently?
> 
> 
> > opendir (DIR, $directory) or die $!;
> > @ARGV = readdir(DIR);
> > 
> > chdir( $directory ) or die $!; #Need to do this readdir has just
> > filename
> > @ARGV = grep( -f, @ARGV);
> > 
> > foreach (@ARGV) {
> >     local $/ ;
> >     open OUT,">$bigfile" or die $!;
> 
> 
> $bigfile will contain ONLY the last file from @ARGV, which isn't
> what you said it is doing.
> 
> So then, the code you have is not the code you've shown us.
> 
> We cannot help fix code that we cannot see...
> 
> 
> > Now this does read the first 225 characters from each file but I need
> > to put it into a single file, not to STDOUT
> 
> 
> Then open() some other filehandle and print() to the other filehandle.



Ok, ok, sorry about trying to change things a little for the post but
I had named a couple files something I did not want to post. Will
never to that again. I just won't use such descriptive filenames.

So, with that said, here is the final part of the code I need to
finish.

my $outf = 'c:\mailme.txt';
foreach (@ARGV) {
    $files = shift(@ARGV);
    open(INFILE,"<$files") or die "Can't open: $!\n";
    open OUTF,">$outf" or die $!;
    read(INFILE,$buff,225,0);
    print OUTF $buff,"\n";
    print "$buff\n";
}
close(INFILE);

This code is supposed to read the first 225 characters from each file
in ARGV.
It seems to do just that in that print $buff does print just like
that. But when I try to print OUTF I only get one line. What am I
missing?
0
tsciortino
4/19/2004 2:41:20 AM
tsciortino@hotmail.com (Tony) writes:

> Tad McClellan <tadmc@augustmail.com> wrote in message news:<slrnc85l8q.1mn.tadmc@magna.augustmail.com>...
> > Tony <tsciortino@hotmail.com> wrote:
> > > 
> > > This does cat all file together into one bigfile
> > 
> > 
> > No it doesn't.
> > 
> > It stomps on $bigfile for each little file...

[ snip ]

> So, with that said, here is the final part of the code I need to
> finish.

[ code that still re-opens output file on each iteration ]

> What am I missing?

What Tad said.  Oh and you didn't close the output file.  This
probalby won't matter unless you try to re-read it before the script
terminates.

Also tsciortino@hotmail.com (Tony) writes:

> foreach (@ARGV) {
>     $files = shift(@ARGV);
      # do stuf...
> }

This is rather confused.  I'm sure you meant either...

  foreach my $files (@ARGV) {
      # do stuff...
  }

Or...
 
  while (@ARGV) {
      my $files = shift(@ARGV);
      # do stuff... 
  }

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\
0
Brian
4/19/2004 8:03:44 AM
"John W. Krahn" <krahnj@acm.org> wrote in message news:<40833037.8B889B34@acm.org>...
> Tony wrote:
> > 
> > I am trying to concatenate all files in a directory together into one
> > file and read the first 225 characters of each file into one file as
> > sort of a summary file.
> > 
> > This does cat all file together into one bigfile
> > 
> > my $directory = "c:\\myfiles";
> > my $bigfile =c:\\bigfile.txt";
> > 
> > opendir (DIR, $directory) or die $!;
> > @ARGV = readdir(DIR);
> > 
> > chdir( $directory ) or die $!; #Need to do this readdir has just
> > filename
> > @ARGV = grep( -f, @ARGV);
> > 
> > foreach (@ARGV) {
> >     local $/ ;
> >     open OUT,">$bigfile" or die $!;
> >     while( <> ){
> >         print OUT $_, "\n";
> >         }
> >     }
> > close OUT;
> > closedir(DIR)
> > 
> > Now this does read the first 225 characters from each file but I need
> > to put it into a single file, not to STDOUT and I can not get the two
> > to run together in the same script.
> > 
> > chdir( $directory ) or die $!;
> > @ARGV = grep( -f, @ARGV);
> > $files = shift(@ARGV);
> > while (<>) {
> > open(INFILE,"<$files") or die "Can't open: $!\n";
> > read(INFILE,$buff,225,0);
> > print "$buff\n";
> > }
> > close(INFILE);
> 
> 
> This should work (untested):
> 
> my $directory = 'c:/myfiles';
> my $bigfile   = 'c:/bigfile.txt';
> my $summary   = 'c:/summary.txt';
> 
> opendir DIR, $directory or die $!;
> @ARGV = map "$directory/$_", grep -f, readdir DIR;
> 
> open OUT, '>', $bigfile or die "Cannot open $bigfile: $!";
> open SUM, '>', $summary or die "Cannot open $summary: $!";
> 
> $/ = \225;
> while ( <> ) {
>     print SUM if $. == 1;
>     print OUT;
>     close ARGV if eof;
>     }
> 
> 
> 
> John

Here is the final answer for reading the first 225 characters from
each file in my directory and creating a file from that.

open OUTF,">$sum" or die "Can't open file $!"; 
 foreach my $files (@ARGV) { 
  open(INFILE, $files); 
  read(INFILE,$buff,225,0); 
  print OUTF $buff, "\n"; 
}
0
tsciortino
4/19/2004 3:28:28 PM
Reply: