Solved

directory count for filetype

Posted on 2008-09-29
16
251 Views
Last Modified: 2013-12-24
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
0
Comment
Question by:dgrafx
  • 7
  • 6
  • 2
16 Comments
 
LVL 11

Accepted Solution

by:
BrianMM earned 500 total points
Comment Utility
Hi,

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">
SELECT *
FROM qryDirectoryList
WHERE Name LIKE '%.jpg'
</cfquery>

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

HTH :)
0
 
LVL 11

Expert Comment

by:BrianMM
Comment Utility
P.s. small typo! - obviously in the <cfquery ..> that's DBTYPE="query"
0
 
LVL 24

Author Comment

by:dgrafx
Comment Utility
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
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Here the RegExp solution:


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

<cfoutput>#myList.RecordCount#</cfoutput>

Open in new window

0
 
LVL 24

Author Comment

by:dgrafx
Comment Utility
I need to count multiple filetypes from ONE query (cfdirectory) so the results cannot be filtered

thanks
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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???

0
 
LVL 24

Author Comment

by:dgrafx
Comment Utility
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 ...
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
OK, that explains the question.
Hope to be of more help when you need more details for the new approach.

See you.
0
 
LVL 24

Author Comment

by:dgrafx
Comment Utility
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.
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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.

0
 
LVL 24

Author Comment

by:dgrafx
Comment Utility
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?
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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?
0
 
LVL 24

Author Comment

by:dgrafx
Comment Utility
sure
case one : ".jpg"
case two : ".jpg,.gif"
case three : ".jpg,.gif,.png"
et cetera
0
 
LVL 24

Author Comment

by:dgrafx
Comment Utility
ok - thats fine
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Hello dgrafx: http:Q_23786327.html
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now