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

Posted May 7, 2008 at 9:47 AM by Ben Nadel

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:

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




Reader Comments

May 7, 2008 at 10:32 AM // reply »
7 Comments

No problem, thanks for the mention.


May 7, 2008 at 10:53 PM // reply »
10 Comments

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'?


May 8, 2008 at 1:41 AM // reply »
9 Comments

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

<?>


May 8, 2008 at 7:18 AM // reply »
11,243 Comments

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


ike
Nov 24, 2008 at 7:11 PM // reply »
78 Comments

They're different because they were done by different programmers at different times and they weren't really paying attention to what had come before... which happens all the time in software development. Why do we have <cffile actuion="upload" /> but instead of <cffile action="download" /> we're forced to use <cfcontent />? That's always seemed pretty incongruous to me honestly. :)

And oddly enough, the comma in cfzip has the potential to cause problems because file names can contain commas. You and I may not be tempted to include a comma in a file name, but that doesn't mean users won't upload word docs with commas in their names that end up in zip files or potentially are already in a zip file before they're uploaded. The pipe would have been safer, although I'm not certain it's not also allowed in a file name... though it is much less likely to find it's way into the file name for a word doc for example.


Nov 24, 2008 at 7:19 PM // reply »
11,243 Comments

@Ike,

That's true - I've definitely seen people put commas in their file names, especially when they have sort of internal versioning methodology:

document,FG18,7,3.doc


Jan 7, 2010 at 1:24 PM // reply »
2 Comments

I guess this feature (using the pipe) will not work for mx7???


Jan 7, 2010 at 1:49 PM // reply »
11,243 Comments

@Brooks,

I am not sure. I don't have any version of CF7 to test this on.


Feb 16, 2010 at 4:31 PM // reply »
7 Comments

Sorry for finding this post so late, but I thought I'd try and ask a question since the post still seems to be open. I'm on CF 8 and wanted to know whether cfdirectory's filter works for a partial name, will it work if you enter something like:

filter="myfile*.cfm"
or
filter="*myfile.cfm".

I didn't see anything in the CF Docs.

Thanks,

JW


Feb 17, 2010 at 5:03 PM // reply »
11,243 Comments

@James,

Yes; these two constructs can be used in that manner as well as in conjunction:

filter="test*.cfm"

... and with the pipe:

filter="test*.cfm|trace"


Feb 22, 2010 at 4:44 PM // reply »
47 Comments

I was trying to remember how the multiple filter thing worked since I don't use it that often and ran across this post which seems to be somewhat alive. I wanted to mention that the version of Railo that i'm running 3.1+ uses the pipe for multiple delimiters as well in case anyone was interested.


Feb 22, 2010 at 7:38 PM // reply »
11,243 Comments

@Hatem,

Oh cool, good to know.


Jul 5, 2010 at 7:49 PM // reply »
2 Comments

How early did ColdFusion begin supporting this vertical bar in the filter attribute of <cfdirectory>? Perhaps it started in the MX version?

FYI: I've tested it in CF6 (v6.1) and it works there too.

Is it grand? Rich and skippy.


Jul 20, 2010 at 9:55 PM // reply »
11,243 Comments

@ArdMan,

Yeah, it makes sense that it would be supported as far back as when they switch to Java. Thanks for testing it on an older system. ColdFusion is groovy!


Jul 1, 2011 at 9:24 AM // reply »
5 Comments

I think I know why Adobe doesn't include this in the docs: this produces unexpected behavior.

For example, if you are filtering filter="*.html|*.cfml|*.htm|*.cfm" results for all extensions are returned, but if you do filter="*.htm|*.cfm|*.html|*.cfml">, only the first 2 are returned.

Meaning the subset filters have to go last - and that's not straightforward enough.

Not sure why this is happening, Ben is the Regex Master, so he can probably tell.


Feb 9, 2012 at 10:35 PM // reply »
3 Comments

I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf.


Dec 21, 2012 at 2:46 PM // reply »
1 Comments

Thanks for the info on this. We had an instance where the extension of a group of files was upper-case for some and lower-case for others. With a single filter this would cause some files to be missed.



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
May 23, 2013 at 5:19 AM
Ask Ben: Print Part Of A Web Page With jQuery
How to print also the background color of table cells and table lines ... read »
May 23, 2013 at 3:55 AM
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
very interesting and helpful too. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools