Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hi,

If I may give you a head up. There are several ways to achieving your aim and this is one of them.

  • Using the module File::Find, you can transverse the whole of the directory ( or directories ), then
  • get each of the file, one after another, read through, using a while loop, within an open function, then do whatever you want!
Please see an example below.
use warnings; use strict; use File::Find qw(find); use File::Basename qw(basename); use Carp qw(croak); croak "Usage: perlscript.pl <drectory_name>" unless defined $ARGV[0]; my $directory_name = $ARGV[0]; find( \&work_n_save, $directory_name ); # transverse the directory for + each file sub work_n_save { return if $_ eq '.' or $_ eq '..'; my $filename = $File::Find::name if !-d; ## get file name if not + directory $filename = basename($filename); ## get the file base na +me $filename =~ s/\..+$//; ## remove the file exte +ntion open my $fh_new, '>', $filename . "_new.txt" or croak "can't open +file: $!"; open my $fh, '<', $_ or croak "can't open file: $!"; while ( defined( my $line = <$fh> ) ) { chomp $line; ## do whatever you want print {$fh_new} $line, $/; } }
NOTE: That the script above will save the new file generated within the same directory, where the original files are.
If you don't want that you might have to create another dircetory and move your new file there!( easy too! )
Try/Test this on any of directory you have and see what you get, then re-write to what you want!

Check perldoc File::Find for detail info.
Hope it helps.


In reply to Re: Reading Multiple files by 2teez
in thread Reading Multiple files by maheshkumar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (13)
    As of 2014-09-02 12:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (22 votes), past polls