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">
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.
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>
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With