Solved

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

Posted on 2001-06-13
5
261 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

739 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