directory count for filetype

I'm looking for a solution to count the number of files in a directory of a specific filetype.
I want to know how many jpgs (or any specific filetype) are in the directory (not recursive)

do not post a solution that filters the cfdirectory as I need all files returned
and also don't post a solution that loops through the results and increments a counter when filetype is found

LVL 25
Who is Participating?
BrianMMConnect With a Mentor Commented:

You can just

<cfdirectory directory="[directory]" name="qryDirectoryList" action="LIST">

You can then use QoQ (Queries of Queries) to query the entire result set of the cfdirectory...

<cfquery type="query">
FROM qryDirectoryList
WHERE Name LIKE '%.jpg'

Or perhaps the 'type' attribute from this query... do a cfdump on qryDirectoryList to check what deta is returned! :)

HTH :)
P.s. small typo! - obviously in the <cfquery ..> that's DBTYPE="query"
dgrafxAuthor Commented:
I forgot to add the "QofQ" to the don't post list
I'm really sorry ...

Do you have any ideas along the lines of regex?
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

ZvonkoSystems architectCommented:
Here the RegExp solution:

<cfdirectory directory="#GetDirectoryFromPath(GetTemplatePath())#" name="myList" action="LIST" filter="*.cfm" >

Open in new window

dgrafxAuthor Commented:
I need to count multiple filetypes from ONE query (cfdirectory) so the results cannot be filtered

ZvonkoSystems architectCommented:
There is no such thing as cached directory list of all entries where you can filter afterwards for two or three attributes except you do QofQ as already proposed.
Can you specify a bit more details what you are asking?
And why do you want to speed up the queries by complicating scripting? The filter parameter is exactly for that what you are asking for. And the caching is done by operating system. Or do you have three thousand files in one folder???

dgrafxAuthor Commented:
The reason I didn't provide detailed details on what I want to accomplish is that it would take me a bit to explain and was hoping someone else had needed to do something similar and had already spent time writing a function to do this.

I'm going to accept brians post although I'm not sure if I'll use it.
This isn't something I'm actively working on but ran accross code this morning where I had always planned on working out a better way

thanks ...
ZvonkoSystems architectCommented:
OK, that explains the question.
Hope to be of more help when you need more details for the new approach.

See you.
dgrafxAuthor Commented:
Hi Guys,
Here is the solution that was like I was hoping to find and is what I meant by a regex solution
CF8 to the rescue
I need to start reading the docs more!

<cfset numret=arraylen(REMatch(replace(lcase(fileTypes),",","|","all"), lcase(valuelist(>
the cfdirectory query is named files
fileTypes is a list of file types that the current user is allowed to access - i.e. ".jpg,.gif,.png"
numret is the count I was looking for

in a library may be a collection of files of various types
one user may be able to access .jpgs & .gifs and another user may be able to access psd's as well

The whole thing boils down to wanting to display a count of how many files
You know like "73 files in this Library - displaying 1-20"

This rematch gadget is pretty cool - I imagine there will be awesome uses for it much neater than what I'm doing with it here.

I'd like to reopen this question and award this as the answer.
Now this isn't to save points as I have unlimited.
It's just so that someone who may search and finds this question may not read down this low in the post, but just look at the accepted answer.
Before I do that please give your input as to if you mind or not brianmm.
ZvonkoSystems architectCommented:
When you so care about correct solution then you should care also for the correct question wording.
You definitely asked for ONE SPECIFIC FILETYPE. Down the line you explained that is not directly what you want.

So now please open a new question and state both: your corrected question and your answer.

The accepted answer in this question is the correct answer to your initial question.
And the answer does not depend on what you are going to do with the answer. It has only to be the correct answer and in this case it is.

dgrafxAuthor Commented:
zvonko > the way you are wording it "ONE SPECIFIC FILETYPE" is a bit different than what I actually asked in the first post where I said "a specific filetype" - you are giving it a different meaning.
When I need to ask a question I start with a real basic scenario and see where it goes.
I'm sure you've seen questions where additional info was given down in subsequent posts.
An no - QofQ is not the best solution whether we are talking about one or multiple filetypes.
And yes, it matters not what the end use may be - I was never trying to imply that my use of this validated my answer - I only blogged a bit about my use of it just so you guys had a clearer understanding of what I was doing.
I also stated that I was looking for a regex solution, which if possible for a specific solution, is always faster than a QofQ.

Like I said - I'm not trying to cheat anyone out of points and specifically I would not have bothered IF this had not been a new function in CF8.
Because of this I believe it to be worthy of reopening.
I recall questions from the past (have no idea where they are) that involved this same general concept (except for the end result was different) where a "pre-CF8" work around was given.

Brianmm - I accepted your solution and I can live with it.
And I do care about best solutions.
After reading my reasonings - do you mind if I ask for reopening then accept the regex solution I posted?
ZvonkoSystems architectCommented:
I have tried to test your solution. It did not work for me. Can you show me what test content you used for the var fileTypes?
dgrafxAuthor Commented:
case one : ".jpg"
case two : ".jpg,.gif"
case three : ".jpg,.gif,.png"
et cetera
dgrafxAuthor Commented:
ok - thats fine
ZvonkoSystems architectCommented:
Hello dgrafx: http:Q_23786327.html
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.