CFDirectory Filtering Uses Single Character Wild Card

Posted May 7, 2008 at 8:21 AM

Tags: ColdFusion

Minor tip here, but over the weekend, at cf.Objective(), I was watching a presentation (I think it was Mark Mandel) when I saw that someone was using a ColdFusion CFDirectory filter that had the "?" wild card. I knew that CFDirectory filtering could use the multi-character "*" wild card, but I am pretty sure I didn't know that it could use the single-character wild card. Very cool!. How did this escape my attention? I ran a quick test just to make sure I wasn't misunderstanding what was going on. In this demo, I am going to list all files in a directory followed by a list of only the files that have a three character file name:

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

  • <!--- Get all files. --->
  • <cfdirectory
  • action="list"
  • directory="#ExpandPath( './' )#"
  • listinfo="name"
  • name="qFile"
  • />
  •  
  • <!--- Output file list. --->
  • <cfdump
  • var="#qFile#"
  • label="All Files"
  • />
  •  
  • <br />
  •  
  • <!---
  • Get files that have only THREE characters followed by
  • and extension type.
  • --->
  • <cfdirectory
  • action="list"
  • directory="#ExpandPath( './' )#"
  • listinfo="name"
  • filter="???.*"
  • name="qFile"
  • />
  •  
  • <!--- Output file list. --->
  • <cfdump
  • var="#qFile#"
  • label="File List With Three Characters"
  • />

This gives us the following two CFDump outputs:


 
 
 

 
CFDirectory Of All Files In A Directory  
 
 
 

 
 
 

 
CFDirectory Showing Only Files With Three Letter File Names Due To Single-Character Wild Cards  
 
 
 

Works like a charm. This would definitely have come in handy several times in the past. At least I will know it going forward. I just looked in the ColdFusion 8 documentation to see if this was mentioned and sure enough, it's right there. I guess I have to be better about reading the documentation.

Download Code Snippet ZIP File

Comments (20)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  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

You really do learn something everyday! I just wish you could use mulitple filters, that would really come in hand. filter="*.png,*.gif,*.jpg"

Posted by Dan Vega on May 7, 2008 at 9:14 AM


@Dan,

You can do just that:

<cfdirectory
action="list"
directory="#ExpandPath( './' )#"
name="qFile"
sort="name ASC"
filter="*.pdf|*.zip|*.doc|*.docx|*.ppt|*.pptx|*.pot|*.dot|*.xls|*.xlsx|*.swf|*.rtf" />

Posted by Steve Withington on May 7, 2008 at 9:23 AM


I will have to give that a try, great if it works though. I always remember the docs saying only 1 filter can be applied. Thanks!

Posted by Dan Vega on May 7, 2008 at 9:25 AM


@Steve,

I didn't know that Pipes could be used. Awesome!

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


It works great! This is straight from the docs and probably why I was confused about that > File extension filter applied to returned names, for example, *.cfm. One filter can be applied. Thanks again.

Posted by Dan Vega on May 7, 2008 at 9:31 AM


@Dan,

I just tried it an it works! However, I did not see this in the documentation. Does it mentions Pipes anywhere or demonstrate their use?

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


No mention of pipes anywhere. Is there a secret handshake I need to learn to get the real docs? If so I will practice.

Posted by Dan Vega on May 7, 2008 at 9:37 AM


The | symbol is just an "or" statement in regex isn't it? I see the docs are really really vague on all this. From the docs: "File extension filter applied to returned names, for example, *.cfm. One filter can be applied. "

There's a whole section there on pattern matching, but it never goes into further details about using regex.

http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_d-e_03.html

Posted by Todd Rafferty on May 7, 2008 at 9:41 AM


@Dan - There's no secret handshake, I think this is the case of us developers having to poke Adobe to improve/clarify the documentation.

Posted by Todd Rafferty on May 7, 2008 at 9:42 AM


Todd is correct. The pipe symbol is just an OR statement (not sure if it's regex or not).

However, I just tried it once a long time ago when commas weren't doing the trick.

Posted by Steve Withington on May 7, 2008 at 9:46 AM


? symbol in regex is 0 or 1. So, when you write: ???.* it should match:

ben.txt
ai.txt
i.txt
.txt <-- possible on a *nix box.

...etc.

Posted by Todd Rafferty on May 7, 2008 at 9:48 AM


Be careful. In the CFZip and CFZipParam tags, which also have filtering, the comma works like the pipe. Same thing, slightly different rules, so not sure if saying that this is a regular expression will work.

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


@Todd,

The ? does not work like a regular expression question mark. In my example, ????.* ONLY matches "anna.txt". By your logic, it should also match "ben.txt". These are not regular expressions.

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


@Ben: Could be that my regex-fu is failing or isn't strong enough. On my cheat sheet, it clearly says ? = 0 or 1. Maybe I'm misunderstanding that. I ran ????.* through RegexBuddy and it throws an error saying The quanitifier ? can only be used after a token that can be repeated.

Sooooo... maybe the filter is not regex-based after all.

Posted by Todd Rafferty on May 7, 2008 at 10:00 AM


@Todd,

Its can't be regex, cause in regex, the "." means any character (except line breaks and returns), and in ours, its the literal period. These looks like regex, but don't seem to act that way.

Posted by Ben Nadel on May 7, 2008 at 10:03 AM


Right, because it'd have to be \. to be a literal period. I guess they went for regex-like and then stopped. IMHO, it'd be better if they just implemented a filter based off regex, but I guess they wanted to keep it simple.

Posted by Todd Rafferty on May 7, 2008 at 10:08 AM


@Ben : btw, I went to that link I posted above and posted a comment at the end of the cfdirectory page and provided a link here. Good discussion regardless. Adobe has a comment approval thing, so it'll be awhile before it shows up.

Posted by Todd Rafferty on May 7, 2008 at 10:09 AM


I wrote a quick tutorial on this
http://www.danvega.org/blog/index.cfm/2008/5/7/CFDirectory-Multiple-Filters

Posted by Dan Vega on May 7, 2008 at 10:11 AM


@Todd,

Agreed - a full regex filter would be awesome!

Posted by Ben Nadel on May 7, 2008 at 10:11 AM


@Dan,

Good stuff :)

Posted by Ben Nadel on May 7, 2008 at 10:13 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