Skip to main content
Inspiring
August 11, 2009
Question

Loop though directories and files?

  • August 11, 2009
  • 1 reply
  • 4104 views

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

    This topic has been closed for replies.

    1 reply

    Inspiring
    August 11, 2009

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

    matthiscoAuthor
    Inspiring
    August 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>

    August 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