×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to get Perl to read all the files in a folder and print to screen?

How to get Perl to read all the files in a folder and print to screen?

How to get Perl to read all the files in a folder and print to screen?

(OP)
Im trying to get perl look in a folder and print to the screen all the file names located in that folder.  Is this posible, if not how can I get this done????

RE: How to get Perl to read all the files in a folder and print to screen?

$folder=$fullfilepath;
opendir (FOLDER, $folder) ¦¦ die "Unable to open your folder.";
@filenames= readdir(FOLDER);
closedir(FOLDER);

This is what I use for a member utility.  The files in that directory will be read into the array @filenames.  Then you can simply use a foreach loop to print each filename to the screen.  You could add

open(FILE,$file);
$size= -s FILE;

inside the loop to print out the size of each file too.

RE: How to get Perl to read all the files in a folder and print to screen?

(OP)
thanks a lot but I went out to buy a Perl book and I already got it done.  However, I would like to know how to put this code inside a html to make it viewable on the web.

So when i click on a link, it "prints" those filenames to the webpage....

RE: How to get Perl to read all the files in a folder and print to screen?

How about a link to the script? You'll need some way of specifying the path of the directory to show if want to display more than one. I think that should work shouldn't it? You might need to use the full path to the script...

Or as an input on a form, when users hit the "submit" they start the script. You could then use a hidden input from each page to pass the script the directory to open and read it from $ENV.

e.g.

<a href="scriptname.pl">Click me to see directory files</a>

<form method=post action="myscript.pl">
<input type=hidden name=dirtoopen value="/users/home/loon">


Hope that helps
Loon

RE: How to get Perl to read all the files in a folder and print to screen?

(OP)
yeah but when I do this
(<a href="scriptname.pl">Click me to see directory files</a>)
it simply spits out the contents of the perl file.  the thing is I want it to display the directory files in a particular table cell.  So when I click on the link it simply shows the directory file names in that cell.  Do I have to write the whole html file in perl, "printing" out all the html tags eg. (print "<html>") and so on...?  Or is there an easier way of doing so?  Thats what I'm looking for an easier way... cause I already have the html file created I just want to run this damn script and have it show in a particular table...  HELP!!!!!!!!!!

RE: How to get Perl to read all the files in a folder and print to screen?

I would rewrite the whole page so that it is a perl CGI script, using perl to output all of the required HTML tags.  To make things real easy, I would use the CGI module that comes with perl.  You create a new CGI object that is your page.  Then, when you want to print some HTML you use the CGI objects functions to print it.  For example, instead of "print '<B>some text</B>'", instead you can "print $MyCGI->bold('some text')".  If you want to print the HTML header, you "print $MyCGI->header".  It makes things very easy.

Otherwise, it sounds like what you are saying is "I have a table displayed. I want to click a link and update the table displayed on the current page with the file listing".  As discussed in another thread, you can't do this...  The solutions offered so far will replace your current page with the output from the CGI script.

So, I'd rewrite the whole page in perl.  In the long run, you are going to see a lot of benefits.

Before you do, though, you need to set up your web server so that it executes perl programs (with a .pl extension) instead of passing the contents of the script to the browser.  What web server are you using?




--
0 1 - Just my two bits

RE: How to get Perl to read all the files in a folder and print to screen?

(OP)
ok SO you're saying that theres no way to do it without rewriting the the page.  Ok then I guess I'll have to do it that way then, I was just trying to avoid doing that.  But I guess I'll have to, thanks.  Im using IIS for my web server...


thanks again

RE: How to get Perl to read all the files in a folder and print to screen?

You have another dangerous option.  You could make the directories you are interested in sub-directories of your web server root dir.  And, don't put any index.html files in the sub-dirs.  The browser will then simply display the files in each sub-dir.  Be aware that this gives your user public a lot of info about your server.   I would not do it that way unless I had a very small and trustworthy user community.  

My vote, follow AndyBo's advice and rewrite the page with CGI.pm.  You'll get a big bang for the investment buck spent on doing that.




keep the rudder amid ship and beware the odd typo

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close