coldfusioncfdirectory

Display Directories and their files


I am attempting to group the output of all directories in a specified path as well as the files within each respective directory. Currently, I have:

    <cfset local.baseWebPath = ExpandPath( "./upload/views/files/" ) />
    <cfset custID = session.authUser.getuser_custid() />
    <cfset filters = "*.pdf|*.zip|*.doc*|*.ppt*|*.pps*|*.ai*|*.eps*|*.xls*|*.swf|*.rtf|*.psd|*.jpg|*.png|*.tif">

    <!---display directory for specific customer--->
        <cfdirectory 
            action = "list"
            directory = "#local.baseWebPath##left(custID, 5)#"
            name = "getFiles"
            filter = "#filters#" 
            recurse = "no"
            />
<cfoutput query="getFiles">
     <!---get the file name--->
     <cfset fname = ListFirst(getFiles.name, "." )>
     <!---get the file extenstion--->
     <cfset exten = ListLast(getFiles.name, ".")>
     <cfif getFiles.type eq "file" and getFiles.name neq "thumbs.db">
          <tr>
               <td align="absmiddle"><a href="#buildUrl('main.attachDownload?filename=#getFiles.name#')#">#fname#</a></td>
               <td align="absmiddle"><img src="/art/assets/images/fileTypes/#fileIcon#" title="#exten#"/></td>
               <td align="absmiddle">
                    <!---convert the file size from bytes into mb and kb--->
                    <cfif getFiles.size GT "1000000">
                         #numberformat(getFiles.size*.000001, 9.99)#Mb
                    <cfelse>
                         #numberformat(getFiles.size*.001, 9)#kb
                    </cfif>
               </td>
               <td align="absmiddle">#getFiles.dateLastModified#</td>
               <td align="absmiddle"><a href="#buildUrl('main.deleteFile?filename=#getFiles.name#')#" onClick="alert('Are you sure you want to delete this file?')"><img src="/art/assets/images/delete.png" title="delete file" /></a></td>
          </tr>
      </cfif> 
</cfoutput>    

    <!---display all directories and their files for admin--->
        <cfdirectory 
            action="list" 
            directory="#local.baseWebPath#" 
            name="allDirectories" 
            recurse="true"
            />

Diplaying the individual customer directory works well. However, when the admin is logged in, all directories should be displayed as well as files grouped by customer. How can I achieve this?


Solution

  • You could use a Query of Queries to sort correctly

    <cfquery name="getFiles" dbtype="query">
    SELECT name, size, datelastmodified, directory
    FROM getFiles
    ORDER BY directory
    </cfquery>
    

    You can then use

    <cfoutput query="getFiles" group="directory">
     #getFiles.directory# <!--- format to show only the custID --->
      <cfoutput>
       <!--- outupt table here --->
      </cfoutput>         
    </cfoutput>