CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)

Posted May 7, 2008 at 9:47 AM

Tags: ColdFusion

After I just posted about how the CFDirectory tag can use the single-character wild card, "?", in its filtering, Steve Withington came in and demonstrated that you can also use the pipe character, "|", to use multiple filters in the same CFDirectory tag. After reading this, I went to the ColdFusion 8 docs but didn't see this listed or mentioned anywhere. As Dan Vega pointed joked, what's the secret handshake you need to get at this kind of information??

Anyway, I ran a quick test, based on my original post, just to see if this worked. In the following demo, I am filtering on files that have the name ben or anna:

 Launch code in new window » Download code as text file »

  • <!---
  • Get all the files that are have either the name ben
  • or anna in them. To separate out the values, we are
  • going to use the pipe "|" character.
  • --->
  • <cfdirectory
  • action="list"
  • directory="#ExpandPath( './' )#"
  • listinfo="name"
  • filter="anna.*|ben.*"
  • name="qFile"
  • />
  •  
  • <!--- Output file list. --->
  • <cfdump
  • var="#qFile#"
  • label="File List With Ben Or Anna"
  • />

Running the above code, I get the following CFDump output:


 
 
 

 
CFDirectory Filter Can Use Pipe Character,  
 
 
 

Works quite nicely, like a regular expression. What's odd, though, is that if you look at the CFZip and the CFZipParam tag, they also have a Filter attribute, but they use the comma to separate out different filter values. I wonder why the difference in things that are doing almost the exact same thing.

Download Code Snippet ZIP File

Comments (4)  |  Post Comment  |  Ask Ben  |  Permalink  |  Print Page




Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader Comments

No problem, thanks for the mention.

Posted by Steve Withington on May 7, 2008 at 10:32 AM


How interesting! Though I did know about the ? wildcard (it is in the docs), the ability to apply several filters with the | delimiter is a very nice surprise!
And very timely, too, as I have been working with cfdirectory just yesterday and had to end up using the tag twice to get only pdf and zip files out of a dir... Will go re-write that cfc now!

I have tested it in CFMX7 and it works there, too.

However, checking the docs, as you did, I see that the FILTER attribute in the CF8 CFML Reference (PDF) is described as:

"File extension filter applied to returned names, for example, *.cfm. One filter can be applied."

Since it states that ONE filter can be applied, how likely is it that this undocumented feature may not be supported in future releases? Or is it just bad wording for 'you can't use filter attribute more than once in the tag'?

Posted by Azadi Saryev on May 7, 2008 at 10:53 PM


Ben ...

Do you know of a good reason why Adobe doesn't put ALL the ColdFusion information regarding features & benefits in the docs?

It's not as if I am at the level of programming competency to leverage these features to my benefit yet ...

But it would be nice to know that there is a way to research and discover these useful assets ... individually ...

What if ... Ben, Dan, Ray, Charlie ... on and on ... weren't available ...

<?>

Posted by Edward Beckett on May 8, 2008 at 1:41 AM


@Azadi,

I am on ColdFusion 8 and it works, so that "one filter" is not accurate. Maybe this is undocumented... or maybe it was just not documented well. I am not sure.

@Edward,

I like to think that ColdFusion is so robust and so powerful that missing parts of the documentation is just inevitable :) As far as a world with out Ben, Dan, Ray, Charlie... I don't want to think about it.

Posted by Ben Nadel on May 8, 2008 at 7:18 AM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting