views:

436

answers:

5

When using cfdirectory, how could I exclude all cfm files and list all others without specifying the file extensions for all files I want to see, or exclude a specific file like index.html without doing a query of query?

I'm looking for something like the following, notice the filter attribute.

<cfdirectory directory="#ExpandPath('./')#" action="list" name="qryFiles" filter="!index.html" sort="name ASC" listinfo="name">

or:

<cfdirectory directory="#ExpandPath('./')#" action="list" name="qryFiles" filter="!*.cfm" sort="name ASC" listinfo="name">
A: 

I am pretty sure the ! operator will not work in the filter parameter.

I don't see a way of getting around doing query of query or looping through the query with a cfoutput/cfloop and then checking the value of each file name with a cfif/cfcase statement to see if you want it to show up.

Jason
+6  A: 

No, it's not possible to exclude files with cfdirectory alone. The filter attribute only specifies which files to include, with DOS style wildcards (* and ?).

The simplest solution is probably to filter after the fact with cfquery.

<cfquery name="qryFiles" dbtype="query">
    SELECT * FROM qryFiles
    WHERE name not like '%.cfm'
</cfquery>
Patrick McElhaney
+1  A: 

It may be possible to do this in a java object with..

CreateObject("java", "java.io.File");

..and a filename filter

Personally, I think you would be better off just using a query of queries.

Nick
That is probably what cfdirectory uses internally. But I agree the QoQ would be the simpler option.
Leigh
+4  A: 

The filter attribute is useless if you're trying to do a an exclusion. Case in point: Just yesterday I wanted to use cfdirectory to grab all the subdirectories but EXLCUDE any directory that started with a period "." so that I could exclude things like ".svn" and ".git". Needless to say I searched all over the place and couldn't find an answer.

In the end I ended up just using some conditional logic in my loop:

<cfloop query="mydir">
  <cfif left(name, 1) neq ".">
    <!--- do some code --->
  </cfif>
</cfloop>

Which got the job done. Of course I could of used a QoQ, but to me adding all that overhead to filter out directories that began with a period seemed silly.

Bottom line is that, yes, we're screwed when it comes to exclusion filtering with cfdirectory, but there's no reason you can't use your imagination and a little bit of code to get the results that you want.

rip747
That's the problem I was afraid of. I was hoping to not have to use all kinds of cfif or QoQ statements to get what I want. Maybe Adobe will have a surprise for us in CF9. I wonder if Railo or OpenBD have better filters for the cfdirectory tag?
Jayson
yeah it sucks to add all this extra logic, but if you package it all up into a udf and call that from your template, the feeling of prison rape goes away O_o
rip747
That's part of why I prefer to use QofQ. You can wrap the code in a UDF that returns what you really wanted from CFDirectory.
Patrick McElhaney
@Jayson, you ask about Railo and filters: Railo lets you use a function for the filter. It is passed the file path and should return true if the file should be included in the result or false it the file should be excluded.
Sean Corfield
+2  A: 

You could create a custom tag that ran the CF directory then looped over the results (like you have) building up a new query or a structure with your results in. That would make is slightly more re-usable in other situations.

Ian
That's a good idea too. A function that takes a filter as a parameter and does a cfdirectory + QoQ.
Jayson