CFLib.org – Common Function Library Project

DirectoryList(directory[, filter][, sort][, recurse])

Last updated April 08, 2004

author

Raymond Camden

Version: 2 | Requires: CF6 | Library: CFMLLib

Description:
Mimics the cfdirectory, action="list" command. Also adds recursive list.

Return Values:
Returns a query.

Example:

<cfscript>
dirList = directoryList("c:\temp","","name desc");
</cfscript>
<cfdump var="#dirList#">

Parameters:

Name Description Required
directory The directory to list. Yes
filter Optional filter to apply. No
sort Sort to apply. No
recurse Recursive directory list. Defaults to false. No

Full UDF Source:

<!---
 Mimics the cfdirectory, action=&quot;list&quot; command.
 Updated with final CFMX var code.
 Fixed a bug where the filter wouldn't show dirs.
 
 @param directory      The directory to list. (Required)
 @param filter      Optional filter to apply. (Optional)
 @param sort      Sort to apply. (Optional)
 @param recurse      Recursive directory list. Defaults to false. (Optional)
 @return Returns a query. 
 @author Raymond Camden (ray@camdenfamily.com) 
 @version 2, April 8, 2004 
--->
<cffunction name="directoryList" output="false" returnType="query">
    <cfargument name="directory" type="string" required="true">
    <cfargument name="filter" type="string" required="false" default="">
    <cfargument name="sort" type="string" required="false" default="">
    <cfargument name="recurse" type="boolean" required="false" default="false">
    <!--- temp vars --->
    <cfargument name="dirInfo" type="query" required="false">
    <cfargument name="thisDir" type="query" required="false">
    <cfset var path="">
    <cfset var temp="">
    
    <cfif not recurse>
        <cfdirectory name="temp" directory="#directory#" filter="#filter#" sort="#sort#">
        <cfreturn temp>
    <cfelse>
        <!--- We loop through until done recursing drive --->
        <cfif not isDefined("dirInfo")>
            <cfset dirInfo = queryNew("attributes,datelastmodified,mode,name,size,type,directory")>
        </cfif>
        <cfset thisDir = directoryList(directory,filter,sort,false)>
        <cfif server.os.name contains "Windows">
            <cfset path = "\">
        <cfelse>
            <cfset path = "/">
        </cfif>
        <cfloop query="thisDir">
            <cfset queryAddRow(dirInfo)>
            <cfset querySetCell(dirInfo,"attributes",attributes)>
            <cfset querySetCell(dirInfo,"datelastmodified",datelastmodified)>
            <cfset querySetCell(dirInfo,"mode",mode)>
            <cfset querySetCell(dirInfo,"name",name)>
            <cfset querySetCell(dirInfo,"size",size)>
            <cfset querySetCell(dirInfo,"type",type)>
            <cfset querySetCell(dirInfo,"directory",directory)>
            <cfif type is "dir">
                <!--- go deep! --->
                <cfset directoryList(directory & path & name,filter,sort,true,dirInfo)>
            </cfif>
        </cfloop>
        <cfreturn dirInfo>
    </cfif>
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson