Skip to main content
May 26, 2006
Question

<CFTREE> display directories folders

  • May 26, 2006
  • 3 replies
  • 687 views
Is there a way to display all the folders in a directory rather then the files (it can display the files as well if it must, but i need all the folders) using a <cftree>. I wasn't sure if you could use a <cfdirectory> and somehow query the folders. Any help would be great.

~Jamie
This topic has been closed for replies.

3 replies

Inspiring
May 26, 2006
you know, your right, i failed to read all of it...

type is a list column... use like datelastmodified and size in the display example below.


<!---
Filename: SimpleFileExplorer.cfm
Author: Nate Weiss (NMW)
Purpose: Provides an interface for exploring files and subfolders
within the ows root
--->


<!--- The user can explore this folder and any nested subfolders --->
<!--- Assume that the parent of the folder that contains this ColdFusion --->
<!--- page (that is, the "ows" folder) should be considered explorable --->
<CFSET BaseFolder = ExpandPath("../")>

<!--- The SubfolderPath variable indicates the currently selected folder --->
<!--- (relative to the BaseFolder). Defaults to an empty string, meaning --->
<!--- that the BaseFolder will be current when the page first appears --->
<CFPARAM NAME="SubfolderPath" TYPE="string" DEFAULT="">


<!--- Utility function to return the parent of a folder, simply by --->
<!--- treating the supplied path as a CFML list, with / and \ characters --->
<!--- as the delimiters. --->
<CFFUNCTION NAME="GetFolderParent">
<CFARGUMENT NAME="Folder">

<!--- We'll start off assuming that the folder has no parent --->
<CFSET var Parent = Folder>

<!--- If there is at least one element in the list --->
<CFIF ListLen(Folder, "/\") GT 0>
<!--- Remove the last element and return the rest --->
<CFSET Parent = ListDeleteAt(Folder, ListLen(Folder, "/\"), "/\")>
</CFIF>

<!--- Return the result --->
<CFRETURN Parent>
</CFFUNCTION>


<!--- If the user wants to create a folder --->
<CFIF IsDefined("FORM.CreateFolder") AND FORM.FolderToCreate NEQ "">
<CFSET NewDirectory = BaseFolder &
"#FORM.CurrentSubfolderPath#/#FORM.FolderToCreate#">

<!--- Assuming that the folder doesn't already exist --->
<CFIF NOT DirectoryExists(NewDirectory)>
<CFDIRECTORY
ACTION="Create"
DIRECTORY="#NewDirectory#">
</CFIF>

<!--- Now consider the new folder to be the current selection --->
<CFSET SubfolderPath = "#FORM.CurrentSubfolderPath#/#FORM.FolderToCreate#">
</CFIF>


<!--- If the user wants to delete a folder --->
<CFIF IsDefined("FORM.DeleteFolder")>
<CFSET DirToDelete = "#BaseFolder#/#FORM.CurrentSubfolderPath#">

<!--- Attempt to delete the folder --->
<CFDIRECTORY
ACTION="Delete"
DIRECTORY="#DirToDelete#">

<!--- Now consider the deleted folder's parent to be current selection --->
<CFSET SubfolderPath = GetFolderParent(FORM.CurrentSubfolderPath)>
</CFIF>

<!--- This variable, then, is the full path of the selected folder --->
<CFSET FolderToDisplay = BaseFolder & SubfolderPath>

<!--- Is the user currently at the top level? --->
<CFSET IsAtTopLevel = ListLen(SubfolderPath, '/') EQ 0>


<!--- Get a listing of the selected folder --->
<CFDIRECTORY
DIRECTORY="#FolderToDisplay#"
NAME="DirectoryQuery"
SORT="Name ASC"
FILTER="*.*">


<CFOUTPUT>
<HTML>
<HEAD><TITLE>Simple File Explorer</TITLE></HEAD>
<BODY>
<H3>Simple File Explorer</H3>

<!--- Create a simple form for navigating through folders --->
<FORM ACTION="SimpleFileExplorer.cfm" METHOD="Post">

<INPUT
TYPE="Hidden"
NAME="CurrentSubfolderPath"
VALUE="#SubfolderPath#">

<!--- Show the subfolder path, unless already at top level --->
<CFIF SubfolderPath EQ "">
You are at the top level.<BR>
<CFELSE>
Current Folder: #SubfolderPath#

<CFIF DirectoryQuery.RecordCount EQ 0>
<INPUT
TYPE="Submit"
NAME="DeleteFolder"
VALUE="Delete">
</CFIF><BR>
</CFIF>

<!--- Provide a drop-down list of subfolder names --->
Select folder: 
<SELECT NAME="SubfolderPath" onchange="this.form.submit()">

<!--- Provide an option to go up one level to the parent folder, --->
<!--- unless already at the BaseFolder --->
<CFIF NOT IsAtTopLevel>
<CFSET ParentFolder = GetFolderParent(SubfolderPath)>
<OPTION VALUE="#ParentFolder#">[parent folder]
</CFIF>

<!--- For each record in the query returned by <CFDIRECTORY> --->
<CFLOOP QUERY="DirectoryQuery">
<!--- If the record represents a subfolder, list it as an option --->
<CFIF Type EQ "Dir">
<OPTION VALUE="#SubfolderPath#/#Name#">#Name#
</CFIF>
</CFLOOP>
</SELECT>

<!--- Submit button to navigate to the selected folder --->
<INPUT TYPE="Submit" VALUE="Go"><BR>

Create folder:
<INPUT
TYPE="Text"
NAME="FolderToCreate"
VALUE="">
<INPUT
TYPE="Submit"
NAME="CreateFolder"
VALUE="OK"><BR>

</FORM>


<!--- Use Query of Queries (In Memory Query) to get a subset of --->
<!--- the query returned by <CFDIRECTORY>. This new query object --->
<!--- will hold only the file records, not any subfolder records --->
<CFQUERY DBTYPE="query" NAME="FilesQuery">
SELECT * FROM DirectoryQuery
WHERE TYPE = 'File'
</CFQUERY>


<!--- If there is at least one file to display... --->
<CFIF FilesQuery.RecordCount GT 0>
<!--- Display the files in a simple HTML table --->
<TABLE WIDTH="500" BORDER="0" CELLPADDING="2" CELLSPACING="0">
<TR BGCOLOR="CornflowerBlue">
<TH>Filename</TH>
<TH>Modified</TH>
<TH>Size</TH>
</TR>

<!--- For each file... --->
<CFLOOP QUERY="FilesQuery">
<!--- Use alternating colors for the table rows --->
<!--- This is explained in the "Next N" examples from Chapter 21 --->
<CFIF FilesQuery.CurrentRow MOD 2 EQ 0>
<CFSET RowColor = "LightGrey">
<CFELSE>
<CFSET RowColor = "White">
</CFIF>

<!--- Display the file details --->
<TR BGCOLOR="#RowColor#">
<!--- File name --->
<TD WIDTH="250">
#Name#
</TD>
<!--- File modification date and time --->
<TD WIDTH="200">
#DateFormat(DateLastModified, "m/d/yyyy")#
at
#TimeFormat(DateLastModified, "h:mm:ss tt")#
</TD>
<!--- File size --->
<TD WIDTH="50" ALIGN="right">
#Ceiling(Size / 1024)# KB
</TD>
</TR>
</CFLOOP>
</TABLE>
</CFIF>

</BODY>
</HTML>
</CFOUTPUT>
May 26, 2006
can you expanded any more on this?... TYPE D didn't work for me and i've done some quite extensive research and i haven't seen anything about action have a "type d" or "type f" attribute.
Inspiring
May 26, 2006
not sure the exact syntax, but the cfdirectory has a type action which you can choose F for file or D for directory....


i think it is:
<CFDIRECTORY ACTION="TYPE D"
DIRECTORY="#FolderToDisplay#"
NAME="DirectoryQuery"
SORT="Name ASC"
FILTER="*.*">

but not sure