Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Loop though directories and files?

Explorer ,
Aug 11, 2009 Aug 11, 2009

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

3.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 11, 2009 Aug 11, 2009

Take a look at the <cfdirectory> tag.  It even has recursion built in.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 13, 2009 Aug 13, 2009

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 18, 2009 Aug 18, 2009

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 19, 2009 Aug 19, 2009

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 19, 2009 Aug 19, 2009

" 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 20, 2009 Aug 20, 2009

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 20, 2009 Aug 20, 2009
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources