Loop though directories and files?
Copy link to clipboard
Copied
Hi folks,
I'd like to loop through the file structure of a directory on my webserver, to create a document structure.
Does anyone know how I can loop through the folders and documents recursively, using nested loops?
Thanks in advance
Copy link to clipboard
Copied
Take a look at the <cfdirectory> tag. It even has recursion built in.
Copy link to clipboard
Copied
Thanks for your reply.
I've looked at the cfdirectory tag recursion, but I still can't get it to work.
I would like to have a drop down menu for each directory, with the files in that directory listed for each option. So users can select a document and download it.
I've got some code below, but its not wuite right. Can anyone please help?
Thankyou.
<p>Please select a document from the list section(s) below.</p>
<cfset thePath = "c:\inetpub\wwwroot\documents\" & folder>
<cfdirectory name="folders" action="List" directory="#thePath#" sort="Name ASC" recurse="yes">
<form action="##" method="post" class="styleform" >
<cfoutput query="folders">
<cfif type eq "Dir">
<select name="doc" class="standard" style="width:200px;" >
<option value="">Choose a document</option>
<option value="">-----------</option>
<cfif type eq "doc">
<cfoutput>
<option value="">#type# #name#</option>
</cfoutput>
</cfif>
</select>
</cfif>
<input name="submit" type="submit" value="Select" class="button" />
</cfoutput>
</form>

Copy link to clipboard
Copied
try this: (I just moved your code around a bit. (Basically, errors in the logic).
Not sure what you are trying to do. Do you mean to have one drop down list for each folder (a) ?
Or one folder with all the files in it (b) ?
I have assumed you want (a), in which case it would go something like this (not a perfect solution).
You need to write the javascript function yourself, (pretty easy to do).
<p>Please select a document from the list section(s) below.</p>
<cfset thePath = "c:\inetpub\wwwroot\documents\" & folder>
<cfdirectory name="folders" action="List" directory="#thePath#" sort="Name ASC" recurse="yes">
<form action="##" method="post" class="styleform" >
<select name="doc" class="standard" style="width:200px;" onClick="javascript:myFormsubmitscript(event);">
<option value="">Choose a document</option>
<option value="">-----------</option>
<cfset lastdir=''>
<cfoutput query="folders">
<cfif type eq "Dir" and directory neq lastdir>
<option value="" >#directory#</option>
<cfset lastdir=directory>
<cfelseif type eq "doc">
<option value="">#type# #name#</option>
</cfif>
</cfif>
</cfoutput>
</select>
</form>
--- Cheers
Copy link to clipboard
Copied
Thanks for your replies.
I'm after a basic file browser, where the path of the cfdirectory tag is produced dynamically, as you browse through folders, something like the code below, but it doesn't quite work properly.
Any help much appreciated.
<cfset basepath = "c:/inetpub/wwwroot/documents/">
<cfset urlpath = "?area=pubtest">
<cfdirectory name="documents" action="List" directory="#basepath##folder#" sort="Name ASC">
<div class="spacer">
<p>Please select a document from the list section(s) below.</p>
<cfoutput query="documents" >
<cfif type eq "dir">
<a href="#basepath##folder#">#name#</a>
<cfelse>
<a href="documents/#folder#/#doc##newurl#&folder=#name#">#name#</a>
</cfif>
<br />
</cfoutput>

Copy link to clipboard
Copied
" but it doesn't quite work properly."
You are not helping yourself much or thoise trying to help you, by not describing just how you formed your conclusion that it is not quite working properly.
Like what is actually happening then ?
Do you get an error message ?
Do you get results you are not expecting ?
Detailed information in your question will help us help you.
Copy link to clipboard
Copied
Thanksk for your replies, I have this working now with the code below, its a simple file browser that display folders and files.
<h3>Publications</h3>
<cfparam name="browse" default="">
<cfparam name="folder" default="">
<cfparam name="doc" default="">
<cfset urlpath = "?area=pubtest">
<cfif browse neq "">
<cfset folder = folder & browse>
<cfelse>
<cfset folder = folder>
</cfif>
<cfset basepath = "c:/inetpub/wwwroot/documents" & folder>
<p>Please select a document from the below, or browse to another folder</p><p>
<cfoutput>
<p>#basepath#</p>
<cfloop list="#folder#" delimiters="/" index="i">
<a href="#urlpath#&browse=/#i#">#i#</a> >
</cfloop>
</cfoutput></p>
<cfdirectory name="folders" action="List" directory="#basepath#" sort="Name ASC" >
<cfoutput query="folders" >
<cfif type eq "Dir">
<div style="height:50px;">
<a href="#urlpath#&browse=#browse#/#name#"><img src="images/folder.jpg" /> #name#</a>
</div></cfif>
</cfoutput>
<cfdirectory name="documents" action="List" directory="#basepath#" sort="Name ASC" >
<cfoutput query="documents" >
<cfif type eq "File">
<div style="height:50px;">
<a href="documents#folder#/#name#" style="margin-top:15px;"><img src="images/doc.jpg" /> #name#</a> </div>
</cfif>
</cfoutput>

Copy link to clipboard
Copied
Yeah, I had in mind that your (original) use of select for this context was a bit suspect.
May I suggest as an improvement you might want to look into cftree / cftreeitem.
This would help you to provide a familiar explorer type view of the file system.
Cheers,
Bryn

