Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Comment on

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

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: <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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2017-10-20 22:18 GMT
    Find Nodes?
      Voting Booth?
      My fridge is mostly full of:

      Results (268 votes). Check out past polls.