Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

combining multiple files into one file

by rjsaulakh (Beadle)
on Jun 23, 2006 at 11:07 UTC ( #557141=perlquestion: print w/replies, xml ) Need Help??
rjsaulakh has asked for the wisdom of the Perl Monks concerning the following question:

i have a large no of .htm files in a specified directory and i need to combine all this files into one .htm file i am trying with the following piece of code but it is not able to open the file . please help

#!/usr/bin/perl #use strict; use warnings ; opendir(DIR, "/home/rjs/perl/perlnut/") or die "cant open the specfied + directory $!"; my @file = readdir(DIR); #my $book = final.htm; open(OUT, ">book") or die "Can't create book: $! \n "; + foreach (sort @file) { local(*IN); open( IN, $_) or die "cant raman open $_ : $!\n "; print OUT while <IN>; close (IN); + }

Replies are listed 'Best First'.
Re: combining multiple files into one file
by jwkrahn (Monsignor) on Jun 23, 2006 at 11:38 UTC
    readdir only returns the file name not the complete path so you have to prepend the path when you want to open the file:
    open IN, "/home/rjs/perl/perlnut/$_" or die "cant raman open /home/rjs +/perl/perlnut/$_ : $!\n ";
Re: combining multiple files into one file
by GrandFather (Sage) on Jun 23, 2006 at 11:20 UTC

    You don't say what error you are getting.

    I notice too that you have commented out use strict; - why?

    Lastly, concatenating HTML files together into one large file is unlikely to be what you actually want to do. It almost certinally will not result in a valid HTML file.

    DWIM is Perl's answer to Gödel
Re: combining multiple files into one file
by rev_1318 (Chaplain) on Jun 23, 2006 at 13:36 UTC
    A few additional remarks:
    * readdir gives you all the 'files' in the directory, including ., .., directories etc.. You should filter out the file you want.
    * Why not use a simple cat *.htm >all.htm ? You get the same useless file with less effort...


Re: combining multiple files into one file
by leocharre (Priest) on Jun 23, 2006 at 14:14 UTC

    My friend, you *must* use strict + warnings... It would make life so much easier for you! In the long run all this extra crap like Taint will become your security blankets intead of mean ol granpa

    This is part of your script fixed up to help you debug: (This code is untested!)

    #!/usr/bin/perl -w use strict; use File::Slurp; my $basedir = '/home/rjs/perl/perlnut'; opendir(DIR, $basedir) or die $!; my @files = readdir(DIR); # name arrays plural, hashes singular closedir DIR; my $outfilename = 'final.htm'; my $outfilesrc = undef; + foreach (sort @files){ $outfilesrc.= File::Slurp::slurp("$basedir/$_"); } open(OUT, "> $basedir/$outfilename") or die ("Can't open for writing: +$basedir/$outfilename : $!"); print OUT $outfilesrc; close OUT; exit;

    File::Slurp might be of interest to you here.

    And for the love of Cindy Lauper and Ayn Rand; doing this in perl on a *ix system would be simply *&%^#%!!. Open a terminal and use cat instead:
    # cat /home/rjs/perl/perlnut/*htm > /home/rjs/perl/perlnut/final.htm

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://557141]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2017-04-29 15:53 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (533 votes). Check out past polls.