Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now