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.
| Viewed 4551 times
#1 by Ben Nadel - November 17, 2010 at 4:55 PM
#2 by Paul Klinkenberg - November 17, 2010 at 5:06 PM
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 by Ben Nadel - November 17, 2010 at 6:15 PM
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 by Anuj Gakhar - December 18, 2011 at 11:15 AM
#5 by Paul Klinkenberg - December 19, 2011 at 12:18 PM