Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Reading Multiple files

by 2teez (Priest)
on Aug 07, 2012 at 13:06 UTC ( #985974=note: print w/ replies, xml ) Need Help??


in reply to Reading Multiple files

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.


Comment on Re: Reading Multiple files
Download Code
Re^2: Reading Multiple files
by maheshkumar (Sexton) on Aug 07, 2012 at 16:21 UTC

    I am not an expert in perl but the code that i have written already defines a file name so what should i do with that? Sorry I did not understand your code completely I am still doing it. Any help on this?

      ..the code that i have written already defines a file name..

      The script I wrote, was on the premises that you want to write your selected "data", from each of the file, to a new file each.
      So, if your intention, then is to use just one file, to get all the selected "data", from various files in the directory ( or directories ), since you said you already have a filename. Then, you might have to modify, your code a bit.
      However, I might be wrong here. Please, if I may clear thing out.
      How are you getting your new files?
      Are you writing, all your output to a single file?
      Or to a new file for EACH file you are reading from?

        Basically what i am doing is that I have around 100 text files with different file names (random names) for example TR1, TR2 and so on. I am extracting data from TR1 and saving it in another file called TR1_Result. I want a code which does this on all files and creates new result file for each of them

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985974]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-07-30 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (236 votes), past polls