Forum Moderators: coopster & phranque

Message Too Old, No Replies

How do I display directory contents

Is this possible with perl?

         

tah1984

1:30 pm on Aug 7, 2003 (gmt 0)

10+ Year Member



I have been searching and searching, but am about to give up.

I'm looking for a script that will scan the directory that the script is sitting in for .shtml files.

I need the script to read the title tag for each file and return the results to an html page with links to the files.

I would also like to have the results listed alphabetically in a table with at least 3 columns.

Thank you for any help I might receive :)

too much information

1:49 pm on Aug 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know if this would help, it's in VB for ASP but it has worked for me. You just call it from a form which asks for the path to the file (ending in '/') and creates a list of files in the directory, including sub-folders.

<%
Sub Main()
sPP = Server.MapPath(".") 'Physical Path
sUP = Request.QueryString("UP") 'URL Path

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(sPP)
Set fc = f.Files
Set ff = f.SubFolders
'--- This lists the sub folders
For Each f1 in ff
Response.Write "<a href=" & sUP & f1.name & ">" & sUP & f1.name & "</a> <br>"
Response.Write sUP & f1.name & "<br>"
Next
'--- This lists the files in the current folder
For Each f1 in fc
fname_split = split(f1.name,".")
if (fname_split(0) <> "dir") then
Response.Write sUP & f1.name & "," & fname_split(0) & "<br>"
end if
Next
Set ff = nothing
Set fso = nothing
Set f = nothing
Set fc = nothing
End Sub

Main()
%>

Storyteller

7:59 pm on Aug 7, 2003 (gmt 0)

10+ Year Member



File::Find from CPAN will serve you well.

tah1984

10:35 pm on Aug 7, 2003 (gmt 0)

10+ Year Member



too much information,
Thank you, but ASP is no good to me I am stuck with cgi :(

Storyteller,
Thank you too, I will have a look and see what I can dig up :)

sugarkane

7:18 am on Aug 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld tah1984

Yep, File::Find is good for recursive directory searches, but you could also look at the simpler opendir(), readdir and closedir() functions too.

As to parsing out the title, you could either go with the HTML::Parse module (this might be a bit of overkill to be honest), or something like this:

open(FP,"filename.shtml");
while ($foo=<FP>) {
$filecontents.=$foo;
}
close(FP);
$content=~s/\rĶ\n//g;
$content=~@<title>.*?</title>@i;
$title=$1;

Do that for every file that your directory search found, put the $title values into an array, sort, and output in the format you want...

Storyteller

6:26 pm on Aug 9, 2003 (gmt 0)

10+ Year Member



sugarkane, your code won't compile. You must explicitly specify regexp operation when using a delimiter other that slash:

 $content =~ [b]m[/b]@<title>.*?</title>@i; 

tah1984

11:05 am on Aug 11, 2003 (gmt 0)

10+ Year Member



Thank you Sugarkane it's good to be here :-)

The problem is that I have 1,000's of shtml files to list. I just want find a way to put links to the files without typing them by hand and I don't really like the way
1word_2word_3word.shtml looks. I would rather
1word 2word 3word

sugarkane

5:55 pm on Aug 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Storyteller - yep, thanks, you're right. That'll teach me to post untested code ;)