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

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

May 7, 2008 at 9:14 AM // reply »
45 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"


May 7, 2008 at 9:23 AM // reply »
7 Comments

@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" />


May 7, 2008 at 9:25 AM // reply »
45 Comments

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!


May 7, 2008 at 9:31 AM // reply »
7,572 Comments

@Steve,

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


May 7, 2008 at 9:31 AM // reply »
45 Comments

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.


May 7, 2008 at 9:35 AM // reply »
7,572 Comments

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


May 7, 2008 at 9:37 AM // reply »
45 Comments

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


May 7, 2008 at 9:41 AM // reply »
188 Comments

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


May 7, 2008 at 9:42 AM // reply »
188 Comments

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


May 7, 2008 at 9:46 AM // reply »
7 Comments

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.


May 7, 2008 at 9:48 AM // reply »
188 Comments

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


May 7, 2008 at 9:52 AM // reply »
7,572 Comments

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.


May 7, 2008 at 9:53 AM // reply »
7,572 Comments

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


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

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


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

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


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

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.


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

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


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

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


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

@Todd,

Agreed - a full regex filter would be awesome!


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

@Dan,

Good stuff :)


Jul 25, 2008 at 1:15 PM // reply »
41 Comments

Okay i ran into a small issue with multiple filters and with office 2007.

If i have my filters set to *.doc|*.docx.... the docx files ARE NOT showing up in the cfdirectory result.

If i swap those two, and make it *.docx|*.doc then both types show up.
Same with xls and xlsx, and i assume ppt and pptx.

Just thought Id share.

-Matthew


Jul 25, 2008 at 1:17 PM // reply »
188 Comments

@Matthew: Good catch, please send that to Adobe as a bug.


Jul 25, 2008 at 1:35 PM // reply »
7,572 Comments

@Matthew,

Awesome catch. Very strange issue.


Jul 25, 2008 at 1:37 PM // reply »
45 Comments

I really hope they are not just checking last 3. Try "osx" and see if that works.


Jul 25, 2008 at 2:48 PM // reply »
41 Comments

@Dan

a filter of *ocx|*.doc will display all .docx and .doc files!!

cheers


Jul 25, 2008 at 2:53 PM // reply »
45 Comments

This makes sense because I have had to warn other programmers about this in CF. I noticed users stripping the file and taking the last 3 of the file name as the extension. A better way is to use ListLast(file,".") and use the period as a delimiter. I have been doing some work in AS3 and you can do this in an array using indexOf. In any case glad you have a little hack for it now!


Jul 25, 2008 at 3:05 PM // reply »
7,572 Comments

@Matthew,

That is just bananas! Was this engine written back when everything had a three-character file extension???


Jul 25, 2008 at 3:09 PM // reply »
41 Comments

Just tried one more thing..

*.doc*|*.txt....

and both .doc and .docx show up that way also.


Jul 25, 2008 at 4:03 PM // reply »
55 Comments

@Matthew,

I like using the wildcard after the 3-letter extension. I've updated my previous post too. Thanks!

http://www.stephenwithington.com/blog/index.cfm/2008/5/7/Use-CFDirectory-to-Filter-Document-Types-by-File-Extension

@Dan,

Can you show a brief example of your suggestion? I'm trying to visualize how this would work and am getting stuck.


Sep 9, 2009 at 11:01 AM // reply »
2 Comments

Does anyone know how ignore a file/folder using cfdirectorya action=list? Let's say I want to list all files (recurse) except for files in the "Archive" directory


Sep 9, 2009 at 11:11 AM // reply »
45 Comments

@Mike D - You would have to do a query of queries to accomplish that. If you are working with a very large recordset you could always dip into Java to accomplish that.


Sep 12, 2009 at 10:54 PM // reply »
7,572 Comments

@Mike,

Agreed with big @Dan - a query of queries is the way to rock out.


Feb 23, 2010 at 3:48 AM // reply »
1 Comments

I just tried a filter param like this:

filter="*uploads_frage*.zip"

and gives me
1 10793_uploads_frage4.zip
2 11650_uploads_frage3.zip
3 2715_uploads_frage8.zip
4 2715_uploads_frage9.zip
5 7383_uploads_frage8.zip
6 7383_uploads_frage9.zip
7 7629_uploads_frage29.zip
8 7629_uploads_frage30.zip
9 7845_uploads_frage10.zip
10 7845_uploads_frage17.zip
11 7845_uploads_frage26.zip
12 7845_uploads_frage9.zip
13 Copy of 7383_uploads_frage9.zip

perfect, so * can be used multiple times in same filter. a step closer to regex ;-)
just wanted to share this with you guys.


Feb 23, 2010 at 9:24 PM // reply »
7,572 Comments

@Raffael,

Very cool - thanks for sharing. Not sure if I ever tried multiple * characters before.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »