<!--- 
 Filename: GetDirectoryContents.cfm
 Created by: Nate Weiss (NMW)
 Purpose: Creates the <CF_GetDirectoryContents> custom tag
 Please Note This example uses Query-of-Queries to combine query objects
--->

<!--- Tag Parameters --->
<cfparam name="ATTRIBUTES.directory">
<cfparam name="ATTRIBUTES.filter" type="string" default="*.*">
<cfparam name="ATTRIBUTES.sort" type="string" default="name">
<cfparam name="ATTRIBUTES.name" type="variableName">

<!--- For each filter (filters separated by semicolons) --->
<cfloop list="#ATTRIBUTES.filter#" index="thisFilter" delimiters=";">
 
 <!--- Get query object of matching files --->
 <cfdirectory action="list" directory="#ATTRIBUTES.directory#"
 filter="#thisFilter#" name="getFiles">

 <!--- If this is first time through loop, --->
 <!--- Save GetFiles as our "ResultQuery". --->
 <cfif not isDefined("resultQuery")>
   <cfset resultQuery = getFiles>
 <!--- If ResultQuery already exists, add ---> 
 <!--- GetFiles's records to it via UNION --->
 <cfelse>
   <cfquery dbtype="query" name="resultQuery">
   SELECT * FROM ResultQuery
   UNION
   SELECT * FROM GetFiles
   ORDER BY #ATTRIBUTES.sort#
   </cfquery>
 </cfif> 
 
</cfloop>

<!--- Return completed resultset to calling template --->
<cfset "Caller.#ATTRIBUTES.name#" = resultQuery>
