Link to home
Start Free TrialLog in
Avatar of dgrafx
dgrafxFlag for United States of America

asked on

directory count for filetype

Hello,
I'm looking for a solution to count the number of files in a directory of a specific filetype.
ex.
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

thanks
ASKER CERTIFIED SOLUTION
Avatar of BrianMM
BrianMM
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
P.s. small typo! - obviously in the <cfquery ..> that's DBTYPE="query"
Avatar of dgrafx

ASKER

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?
thanks
Here the RegExp solution:

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

Open in new window

Avatar of dgrafx

ASKER

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

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

Avatar of dgrafx

ASKER

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 ...
OK, that explains the question.
Hope to be of more help when you need more details for the new approach.

See you.
Avatar of dgrafx

ASKER

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(files.name))))>
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.
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.

Avatar of dgrafx

ASKER

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?
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?
Avatar of dgrafx

ASKER

sure
case one : ".jpg"
case two : ".jpg,.gif"
case three : ".jpg,.gif,.png"
et cetera
Avatar of dgrafx

ASKER

ok - thats fine