Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

leo2180

IS-IT--Management
Jun 7, 2000
52
0
0
US
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????
 
$folder=$fullfilepath;<br>opendir (FOLDER, $folder) ¦¦ die &quot;Unable to open your folder.&quot;;<br>@filenames= readdir(FOLDER);<br>closedir(FOLDER);<br><br>This is what I use for a member utility.&nbsp;&nbsp;The files in that directory will be read into the array @filenames.&nbsp;&nbsp;Then you can simply use a foreach loop to print each filename to the screen.&nbsp;&nbsp;You could add <br><br>open(FILE,$file);<br>$size= -s FILE;<br><br>inside the loop to print out the size of each file too.<br><br>
 
thanks a lot but I went out to buy a Perl book and I already got it done.&nbsp;&nbsp;However, I would like to know how to put this code inside a html to make it viewable on the web.<br><br>So when i click on a link, it &quot;prints&quot; those filenames to the webpage....
 
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...<br><br>Or as an input on a form, when users hit the &quot;submit&quot; 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.<br><br>e.g.<br><FONT FACE=monospace><br>&lt;a href=&quot;scriptname.pl&quot;&gt;Click me to see directory files&lt;/a&gt;<br><br>&lt;form method=post action=&quot;myscript.pl&quot;&gt;<br>&lt;input type=hidden name=dirtoopen value=&quot;/users/home/loon&quot;&gt;<br></font><br><br>Hope that helps<br>Loon
 
yeah but when I do this <br>(&lt;a href=&quot;scriptname.pl&quot;&gt;Click me to see directory files&lt;/a&gt;)<br>it simply spits out the contents of the perl file.&nbsp;&nbsp;the thing is I want it to display the directory files in a particular table cell.&nbsp;&nbsp;So when I click on the link it simply shows the directory file names in that cell.&nbsp;&nbsp;Do I have to write the whole html file in perl, &quot;printing&quot; out all the html tags eg. (print &quot;&lt;html&gt;&quot;) and so on...?&nbsp;&nbsp;Or is there an easier way of doing so?&nbsp;&nbsp;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...&nbsp;&nbsp;HELP!!!!!!!!!!
 
I would rewrite the whole page so that it is a perl CGI script, using perl to output all of the required HTML tags.&nbsp;&nbsp;To make things <i>real</i> easy, I would use the CGI module that comes with perl.&nbsp;&nbsp;You create a new CGI object that is your page.&nbsp;&nbsp;Then, when you want to print some HTML you use the CGI objects functions to print it.&nbsp;&nbsp;For example, instead of &quot;print '&lt;B&gt;some text&lt;/B&gt;'&quot;, instead you can &quot;print $MyCGI-&gt;bold('some text')&quot;.&nbsp;&nbsp;If you want to print the HTML header, you &quot;print $MyCGI-&gt;header&quot;.&nbsp;&nbsp;It makes things very easy.<br><br>Otherwise, it sounds like what you are saying is &quot;I have a table displayed. I want to click a link and update the table displayed on the current page with the file listing&quot;.&nbsp;&nbsp;As discussed in another thread, you can't do this...&nbsp;&nbsp;The solutions offered so far will replace your current page with the output from the CGI script.<br><br>So, I'd rewrite the whole page in perl.&nbsp;&nbsp;In the long run, you are going to see a lot of benefits.<br><br>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.&nbsp;&nbsp;What web server are you using? <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
ok SO you're saying that theres no way to do it without rewriting the the page.&nbsp;&nbsp;Ok then I guess I'll have to do it that way then, I was just trying to avoid doing that.&nbsp;&nbsp;But I guess I'll have to, thanks.&nbsp;&nbsp;Im using IIS for my web server...<br><br><br>thanks again
 
You have another dangerous option.&nbsp;&nbsp;You could make the directories you are interested in sub-directories of your web server root dir.&nbsp;&nbsp;And, don't put any index.html files in the sub-dirs.&nbsp;&nbsp;The browser will then simply display the files in each sub-dir.&nbsp;&nbsp;Be aware that this gives your user public a lot of info about your server.&nbsp;&nbsp;<b> I would not do it that way</b> unless I had a very small and trustworthy user community.&nbsp;&nbsp;<br><br>My vote, follow AndyBo's advice and rewrite the page with CGI.pm.&nbsp;&nbsp;You'll get a big bang for the investment buck spent on doing that. <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top