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

T

Tony

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

Tad McClellan

Tony said:
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.
 
J

John W. Krahn

Tony said:
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
 
T

Tony

Tad McClellan said:
No it doesn't.

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




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';



^^
^^ where's the quote?


Please don't try and retype code.

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




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




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?
 
B

Brian McCauley

[ 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.
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\\
 
T

Tony

John W. Krahn said:
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";
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top