Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Unable to read the sub direcctory using perl?

by gpssana (Initiate)
on Apr 18, 2017 at 03:51 UTC ( [id://1188195]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Unable to read the sub direcctory using perl?
by thanos1983 (Parson) on Apr 18, 2017 at 08:11 UTC

    Hello gpssana,

    Can you read (How do I post a question effectively?) and update your question?

    Help us to help you, what you have tried? Errors? Help us replicate your problem.

    Something like "Unable to read the sub direcctory using perl?" does not mean anything.

    Update: I did a bit of search and I found this tutorial (Directory Recursion) written from the community. I have created a sample of code that reads my directory and sub directory and prints the contents that match my search.

    Sample of code taken and modified from the tutorial:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub get_perl_files { my $path = shift; opendir (my $dh, $path) or die "Unable to open $path: $!"; my @files = map { $path . '/' . $_ } grep { !/^\.{1,2}$/ } readdir ($dh); return grep { (/\.pl/) && (! -l $_) } map { -d $_ ? get_perl_files ($_) : $_ } @files; } my @files_returned = get_perl_files("/home/tinyos/Monks"); print Dumper \@files_returned; __END__ $ perl test.pl $VAR1 = [ '/home/tinyos/Monks/test.pl~', '/home/tinyos/Monks/cmp.pl', '/home/tinyos/Monks/df.pl', '/home/tinyos/Monks/mySuDir/test_subdir.pl', '/home/tinyos/Monks/parallel_ssh.pl', '/home/tinyos/Monks/test_2.pl~', '/home/tinyos/Monks/sshutle.pl', '/home/tinyos/Monks/test_2.pl', '/home/tinyos/Monks/excel.pl', '/home/tinyos/Monks/test.pl', '/home/tinyos/Monks/parsingvalues.pl' ];

    Update2: modifying DIR to my $dh.

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Unable to read the sub direcctory using perl?
by thanos1983 (Parson) on Apr 19, 2017 at 08:23 UTC

    Hello gpssana,

    Your question looks better now, but try to provide us with a bit more information. What is $word on your code, provide us with sample of desired output.

    From what I see from your code, it is not clear for me what you are trying to do. Is it possible to write some high level description, (e.g. step one open my path, step two read directories and search for directories that start with (rev*), step three retrieve all html links files withing the directories file, etc, etc... I am just guessing here, I can not really understand what are you trying to achieve.

    Also when you are trying to come up with a solution focus on the part of your code that is not working. What I mean comment out all parts that they do not play a vital role on your solution yet, focus on the line that you have a problem until you get the desired result and then proceed to the next step not all together.

    Update: I checked the tutorial that I post yesterday, and I am wondering at this point if you read it. I am asking because more or less contains 80%-90% of your desired solution.

    Sample of code modified to meet your criteria, taken from the tutorial.

    # Accepts one argument: the full path to a directory. # Returns: A list of files that end in '.html'. sub get_new_htmls { my $path = shift; opendir (my $dh, $path) or die "Unable to open $path: $!"; my @files = map { $path . '/' . $_ } grep { !/^\.{1,2}$/ } readdir ($dh); # Rather than using a for() loop, we can just # return a directly filtered list. return grep { (/\.html$/) && (! -l $_) } map { -d $_ ? get_new_htmls ($_) : $_ } @files; }

    I created a sample of index.html file trying to replicate your problem (sample of file bellow).

    <html> <head> <title>Sample "Hello, World" Application</title> </head> <body bgcolor=white> <table border="0" cellpadding="10"> <tr> <td> <img src="images/springsource.png"> </td> <td> <h1>Sample "Hello, World" Application</h1> </td> </tr> </table> <p>This is the home page for the HelloWorld Web application. </p> <p>To prove that they work, you can execute either of the followin +g links: <ul> <li>To a <a href="hello.jsp">JSP page</a>. <li>To a <a href="hello">servlet</a>. </ul> </body> </html>

    So I added the file in my directory and simply copied and applied minor minor modifications from the tutorial that I provide you (Directory Recursion).

    Sample of code with output.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub get_new_htmls { my $path = shift; opendir (my $dh, $path) or die "Unable to open $path: $!"; my @files = map { $path . '/' . $_ } grep { !/^\.{1,2}$/ } readdir ($dh); # Rather than using a for() loop, we can just # return a directly filtered list. return grep { (/\.html$/) && (! -l $_) } map { -d $_ ? get_new_htmls ($_) : $_ } @files; } my @returned_files = get_new_htmls("/home/tinyos/Monks"); print Dumper \@returned_files; __END__ perl test.pl $VAR1 = [ '/home/tinyos/Monks/index.html' ]

    Update your question based on the steps that I described above, also provide us with the error message that you are getting and the desired output. Sample of code that I provided you more or less should solve your problem by 90%.

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Unable to read the sub direcctory using perl?
by Anonymous Monk on Apr 18, 2017 at 20:25 UTC
    I always use The File::Find when I am unable to read the sub directory using perl.

    "Developing in Windows is easy just use \ instead of / and / instead of \" -Ron Smith

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found