Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file.

Reply
Thread Tools

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

 
 
Tony
Guest
Posts: n/a
 
      04-18-2004
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..
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-18-2004
Tony <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-19-2004
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
 
Reply With Quote
 
Tony
Guest
Posts: n/a
 
      04-19-2004
Tad McClellan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
> Tony <(E-Mail Removed)> 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?
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      04-19-2004
(E-Mail Removed) (Tony) writes:

> Tad McClellan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
> > Tony <(E-Mail Removed)> 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 (E-Mail Removed) (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\\
 
Reply With Quote
 
Tony
Guest
Posts: n/a
 
      04-19-2004
"John W. Krahn" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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";
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate/De-Concatenate Carlos VHDL 10 10-24-2012 01:04 PM
concatenate several xml files into one big xml/html file? idiotprogrammer XML 4 03-05-2007 06:43 PM
how to concatenate all the lines into one string Li Chen Ruby 3 10-08-2006 06:49 PM
Issue with XML::XSLT and html encoded characters (&#225; etc) Rwag Perl Misc 9 04-23-2006 10:10 PM
How to concatenate 'like' files in a dir? wilson_work@yahoo.com Perl Misc 7 02-20-2006 03:54 PM



Advertisments