We help IT Professionals succeed at work.

dynamically making a link to all files in a specific folder

Judy Deo
Judy Deo asked
on
i have a folder that may contain an unknown number
of files and may be of different file types (i.e .txt, .html, etc.)
what i want to do i create a .cfm page that will make a link to all these files no matter how many there are and
no matter why type of file they are.

So if the folder contained the following:
-------------
blah1.txt
blah4.txt
cars6.html
carparts.html
------------------
then i would want a .cfm page that creates a link
to each one, i.e

<a href="foldername/blah1.txt">blah1.txt</a> <br />
<a href="foldername/blah4.txt">blah4.txt</a> <br />
<a href="foldername/cars6.html">cars6.html</a> <br />
<a href="foldername/carparts.html">carparts.html</a><br />


but the next day there may be 10 files in there instead
of just 4, and some of the files may be .pdf's or .doc's, etc.
Comment
Watch Question

Commented:
<CFSET variables.pathToUse = GetDirectoryFromPath(ExpandPath("*.*"))>
<CFDIRECTORY ACTION="LIST" DIRECTORY="#variables.pathToUse#" NAME="filelist" SORT="datelastmodified DESC">
<CFOUTPUT QUERY="filelist">
     <CFIF left(filelist.name,1) is not ".">
         <a href="#filelist.name#">#filelist.name#</a> (size: #filelist.size#, type: #filelist.type#)<BR>
     </CFIF>
</CFOUTPUT>

Commented:
A Little Modification CJ,

<CFSET variables.pathToUse = GetDirectoryFromPath(ExpandPath("*.*"))>
<CFDIRECTORY ACTION="LIST" DIRECTORY="#variables.pathToUse#" NAME="filelist" SORT="datelastmodified
DESC">
<CFOUTPUT QUERY="filelist">
    <CFIF left(filelist.name,1) is not ".">
        <a href="#variables.pathToUse##filelist.name#">#filelist.name#</a> (size: #filelist.size#, type: #filelist.type#)<BR>
    </CFIF>
</CFOUTPUT>

I hope u dont mind.

Commented:
No prob :-)

I just assumed the same directory since we are expanding the current path.. the relative links should work too.

CJ

Author

Commented:
forgot to mention something.

the directory is on a different domain (i.e another website).

it needs to list the files of
http://www.blah.com/foldername/

(assume the user will have permissions to access that folder)





Commented:
First if the User has permission to see the directory and the directory doesn't have an index/default file then just link to that page (b/c it will display the files in a directory listing manner anyways) or use CFHTTP GET to retrieve the contents and display them on your site (if you want to simulate that they are being kept on your site)

But if the URL and folder has a default file (index.htm, index.cfm, index.asp, etc) then you CAN NOT read the directory contents from it using standard HTTP access.

CJ

Author

Commented:
yes, i want to simulate that they are being kept on my site.
no, there is not default file (i.e index.html, index.cfm, etc.)

so the new code would be?.....

Commented:
very basic way:
<CFHTTP URL="http://www.blah.com/foldername/" METHOD="GET" RESOLVEURL="false">
</CFHTTP>

<CFOUTPUT>#CFHTTP.FileContent#</CFOUTPUT>

CJ
Commented:
If you know the file is in a standard way and pick out delimeters (by viewing source)
you can try to create a query object:
<CFHTTP METHOD = "Get"
URL = "http://www.blah.com/foldername/"
NAME = "files"
COLUMNS = "Name,Size,Date"
DELIMITER = "<BR>"
TEXTQUALIFIER ="""">

Then you can use files as a query object and loop through it using cfoutput.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.