Solved

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

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Migrate OTRS to newest version. 1 326
what are these modules in perl script 4 98
Call Shell Script from Perl Script 6 100
Perl Untar File 1 42
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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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