Solved

Need a help --150 points for this question --Read inner folder contents

Posted on 2001-06-13
5
257 Views
Last Modified: 2008-02-26
Hi all,
I need a help(Urgent).
I would like to read the folders and files from my root directory.
The root directory contain subfolders and subfolders contain subfolders and/or files.
I need to read the contents from the folders/files and stored in variables.
I started coding like this:

#!/usr/bin/perl
print "Content-type:text/html\n\n";
$userDir="/usr/fweb/ourwebb";

$userDir="/usr/fweb/ourwebb";

########Read directory
if(opendir(USER_DIR, $userDir))
     {
          while($dirlist = readdir(USER_DIR))
          {
               print $dirlist;
          }
     close(USER_DIR);
     return 1;
     }

}

Note: While printing the $dirlist, showing the folders as ...logs_suntest3_sureshpFostersRobinShannonsRockwells(starting with .(dot)--for first entry one dot,next 2 dot)

Eg:Say if My root directory contain  folders as ONE,TWO,THREE,FOUR.
Folder ONE contain subfolder ONE1,ONE2 and a demo.txt file. The subfolder ONE1 contain foot.txt file.
Folder TWO contain subfolder TWO1 and b.txt file. The subfolder TWO1 contain TWO12 folder. The subfolder TWO12 contain game.txt file.
Folder THREE contain list.txt file.
Folder FOUR contain xy.txt file.

ie:
ONE -->[ONE1-->(foot.txt),ONE2,demo.txt]
TWO -->[TWO1->(TWO12->game.txt),b.txt]
THREE --> list.txt
FOUR -->  xy.txt

How can i solve this problem? Need a perl code to solve this problem.
I don't have any idea to read the subfolder contents. Please help me to solve this problem as early as possible.

150 POINTS WILL BE AWARDED.

Waiting for the soln.
0
Comment
Question by:sureshp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:ToniVR
ID: 6186600
You have to make a recursive function. As parameter, you have to give the subdirectory, so that you can call it again in the function itself, with another subdirectory.
You can check for a directory with the -d option.

if ( -d $dirlist )
{
   # call your function again with the $dirlist as parameter
}
else
{
   # it's a file
}

Be sure to check if the directory is empty!

I'm going to make such a function, but that will be after the final exams (so if you have time, wait until 27 june).

ToniVR
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 6187544
use File::Find;
find(\&wanted, '/usr/fweb/ourwebb');
sub wanted {
  print $File::Find::name,"\n";
}
0
 
LVL 1

Author Comment

by:sureshp
ID: 6189485
ToniVR,ozo
Could you pls post the eg code to solve this problem?
0
 
LVL 2

Expert Comment

by:ToniVR
ID: 6190390
Sureshp, the code from ozo works perfectly (I tried it here on Win32 with ActivePerl, and it does it's job). Try copy and paste.
And, instead of print ... try $array[i] = $File::Find::name;
0
 
LVL 1

Author Comment

by:sureshp
ID: 6213126
ToniVR,
Ozo's code is working. But i cannot store the first folder value in a variable. My requirement is different.
Anyway i used a lengthy code and finished that work.
Thanks a lot.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Filtering a file to table 9 102
Having a problem with ActiveState Perl on 64-bit Windows 7 3 77
Union rows in array that have common elements 2 109
parse a file and get data out 11 103
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question