Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How do I read the contents of each file in a directory?

by sanjaysingh (Initiate)
on Mar 29, 2006 at 03:36 UTC ( [id://539860]=perlquestion: print w/replies, xml ) Need Help??

sanjaysingh has asked for the wisdom of the Perl Monks concerning the following question:

i am trying to read a DIR and then read the content of each file in that DIR. Kindly help it.
#!/usr/local/bin/perl opendir DH, "Sanjay" or die "fail" ; while($temp = readdir(DH)) { print "$temp\n"; open FILE, "$temp" or die "fail"; print "pass"; while(<$temp>) { print "sucess"; } }

2006-03-29 Retitled by planetscape, as per Monastery guidelines
Original title: 'new to perl need help'

Replies are listed 'Best First'.
Re: How do I read the contents of each file in a directory?
by merlyn (Sage) on Mar 29, 2006 at 03:47 UTC
    The names that come back from readdir are the basename only. You'll have to prepend the directory name to get a good path. Or just use glob, as it's far easier for the beginner.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: How do I read the contents of each file in a directory?
by NetWallah (Canon) on Mar 29, 2006 at 04:25 UTC
    Here is probably what you are looking for.
    my $CurrentDir="."; # Or "Sanjay"; opendir DH, "." or die "failed to readdir $CurrentDir: $!"; while($temp = readdir(DH)) { print "$temp\n"; if ($temp eq "." or $temp eq ".." or -d $temp){ next; # Do not open . or .. } open FILE, "$CurrentDir/$temp" or die "failed to open file $temp:$! +\n"; print "pass"; while(<FILE>) { print "sucess: $_"; } }
    Normally - I would not code this way - this is written to help you get past initial roadblocks.

    UPDATE: Changed while(<$temp>) to while(<FILE>).
    This makes more sense, and is probably what you were trying to do.

    Also, as merlyn says, the "opendir" and "while($temp=readdir..." can be replaced with :

    while (glob "$CurrentDir/*"){ $temp = $_; # Remainder of the code is the same.

         "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

Re: How do I read the contents of each file in a directory?
by gube (Parson) on Mar 29, 2006 at 04:49 UTC
    #!/usr/local/bin/perl use strict; opendir(DIR, "."); my @files = grep/^\w+/, readdir(DIR); for (@files) { undef $/; open(IN, "$_") || die "Cannot open $_"; my $str = <IN>; print $str; close(IN); }

    If you need to read only text file you can grep only .txt file. For eg: my @files = grep/.txt$/, readdir(DIR);

Re: How do I read the contents of each file in a directory?
by mk. (Friar) on Mar 29, 2006 at 14:08 UTC
    is this kind of what you wanted?! no sure i got it right... =/

    perl -pe '' Sanjay/*


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    @

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://539860]
Approved by merlyn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-25 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found