Railo tip: use a custom function for the cfdirectory "filter" attribute

While searching for something else which had to do with cfdirectory, I saw a lot of questions about the filter attribute of cfdirectory. With this attribute, you can filter the results by extensions or part of the file/directory name. For example "*.gif".

The first question that a lot of people have, is whether they can use multiple file filters. The answer is: Yes. You just need to delimit the file filters by a pipe character, like this: "*.gif|*.jpg|*.png".

Another question was whether you could disallow some files or directories from the listing. Now that's much more complicated, or at least you need to write some extra lines of code to remove these from the original directory listing...

Except on Railo! Because with Railo, you can use a custom function as the filter argument, like this:

<cfdirectory action="list" directory="/test/" recurse="true" name="qFiles" filter="#theFilter#" />
<cfdump var="#qFiles#" />

<cffunction name="theFilter" returntype="boolean">
<cfargument name="fullpath" type="string" />
<!--- disallow a certain directory --->
<cfif refindNoCase("[/\\]DisallowedDirectory[/\\]", arguments.fullPath)>
<cfreturn false />
<!--- allow certain extensions --->
<cfelseif refindNoCase("\.(swf|jpe?g|gif|png)$", arguments.fullPath)>
<cfreturn true />
<cfelse>
<cfreturn false />
</cfif>
</cffunction>

As you can see, the function returns a boolean value "true" or "false". True means that the file or directory will be included into the listing, and when False, it will not.

Pretty cool eeh! Start using Railo today ;-)

By the way, when you test this code on ACF 8 or 9, it does not throw an error, but instead just returns zero records.

del.icio.us Digg StumbleUpon Facebook Technorati Fav reddit Google Bookmarks
| Viewed 4641 times
  1. Ben Nadel

    #1 by Ben Nadel - November 17, 2010 at 4:55 PM

    Using a function as a filter is definitely a cool approach! This reminds me of the NoSQL stuff I've seen lately, where people are using Javascript functions to extract records from a document store.
  2. Paul Klinkenberg

    #2 by Paul Klinkenberg - November 17, 2010 at 5:06 PM

    Hi Ben, after I wrote this tip as a comment on your site, I thought "hey, that might be a good new Railo tip", so I now also blogged about it :)
    I'm hoping that these kinds of options will pop up in more tags and functions in cfml, because it makes everything so flexible.
    Btw, you'll be in Edinburgh at Scotch on the Rocks again right? Looking forward to that!
  3. Ben Nadel

    #3 by Ben Nadel - November 17, 2010 at 6:15 PM

    @Paul,

    I think I have your comment floating around in my inbox. Was a conf. last week, so am behind on email :)

    Yeah, this kind of stuff is cool. I would love to see functions used a lot more.

    Heck yeah, I'll be at SOTR :) Looking forward to seeing the castle.
  4. Anuj Gakhar

    #4 by Anuj Gakhar - December 18, 2011 at 11:15 AM

    Thanks for the cool tip! I have come across situations where I need to list everything but one directory so this will come in handy.
  5. Paul Klinkenberg

    #5 by Paul Klinkenberg - December 19, 2011 at 12:18 PM

    @Anuj Gakhar Np! And don't thank me, thank Railo ;)
(will not be published)
Leave this field empty

such