coldfusioncfdirectory

Is it possible somehow to exclude files with cfdirectory?


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">

Solution

  • 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.