Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2001-06-13
5
Medium Priority
?
273 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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

704 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