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

Discussion in 'Perl Misc' started by Tony, Apr 18, 2004.

  1. Tony

    Tony Guest

    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..
     
    Tony, Apr 18, 2004
    #1
    1. Advertising

  2. 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



    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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 18, 2004
    #2
    1. Advertising

  3. Re: Need to concatenate all files in a dir together into one file andread the first 225 characters from each file into another file.

    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
     
    John W. Krahn, Apr 19, 2004
    #3
  4. Tony

    Tony Guest

    Tad McClellan <> wrote in message news:<>...
    > 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

    >
    >
    > 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?
     
    Tony, Apr 19, 2004
    #4
  5. (Tony) writes:

    > Tad McClellan <> wrote in message news:<>...
    > > Tony <> 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 (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\\
     
    Brian McCauley, Apr 19, 2004
    #5
  6. Tony

    Tony Guest

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

    "John W. Krahn" <> wrote in message news:<>...
    > 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";
    }
     
    Tony, Apr 19, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. idiotprogrammer
    Replies:
    4
    Views:
    1,187
    Joseph Kesselman
    Mar 5, 2007
  2. Li Chen
    Replies:
    3
    Views:
    146
    Marc Heiler
    Oct 8, 2006
  3. Replies:
    7
    Views:
    179
    A. Sinan Unur
    Feb 20, 2006
  4. Rwag
    Replies:
    9
    Views:
    317
    robic0
    Apr 23, 2006
  5. Carlos

    Concatenate/De-Concatenate

    Carlos, Oct 12, 2012, in forum: VHDL
    Replies:
    10
    Views:
    961
Loading...

Share This Page