Solved

Count files in one directory filtered by a set of filetypes

Posted on 2008-10-03
9
911 Views
Last Modified: 2012-06-21
How to count the number of files by file type in one directory when not only one file type is searched but two or more file types, e.g.: .jpg,.gif,.png
The file types are not fixed but provided by a variable. Here an example:
<cfset fileTypes=".jpg,.gif,.png" >
Result needed is only the number of files not the list of files.

Best solution will get all points, no split, so be serious that you have a better solution then Query of Queries from a cfdirectory.

0
Comment
Question by:Zvonko
9 Comments
 
LVL 19

Expert Comment

by:erikTsomik
Comment Utility
0
 
LVL 63

Author Comment

by:Zvonko
Comment Utility
Sorry, the cfset example was misleading.
Every user has a different value for his fileTypes.
0
 
LVL 2

Expert Comment

by:daviddriv
Comment Utility
Well, in VBScript, I would split the list of file types into an array, iterate through the array, and search the file list for files with that extension and use a variable to hold the count...  Or, even better, in Linux I would ls -ld *.jpg *.gif *.png | wc -l
0
 
LVL 63

Author Comment

by:Zvonko
Comment Utility
Sorry, I assumed it is obvious from the question Zone: the solution should work on ColdFusion application server and should not depend on OS file system or tools.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 14

Expert Comment

by:SBennett
Comment Utility
If you don't want to do a query of queries, then the best way to get the numbers is probably to cfloop through your list of filters, do a cfdirectory for each file type and then use the record count from the result set. Like this
<cfset fileTypes=".jpg,.gif,.png,.htm" >

<cfloop list="#fileTypes#" index="i">

<cfdirectory action="LIST" directory="#expandpath('/POToOracle')#" filter="*#i#" sort="name" name="results">

<cfoutput>#i# - #results.recordcount# files<br></cfoutput>

</cfloop>

Open in new window

0
 
LVL 63

Author Comment

by:Zvonko
Comment Utility
OK, then sorry again I have not limited the cfdirectory to one call. But that is the requirement.
0
 
LVL 24

Accepted Solution

by:
dgrafx earned 500 total points
Comment Utility
#arraylen(REMatch(replace(lcase(fileTypes),",","|","all"), lcase(valuelist(files.name))))#

where files is the cfdirectory query
and fileTypes is the ".jpg,.gif,.png" list variable

as is common with regex - very fast
0
 
LVL 63

Author Comment

by:Zvonko
Comment Utility
Thanks dgrafx.
For better understanding here the cfdump output from one example:
<title>dgrafx's types</title>
 

<cfdirectory directory="#GetDirectoryFromPath(GetTemplatePath())#vw" name="files" action="LIST" >
 

<cfset fileTypes=".jpg,.gif,.png" >

<cfset numret=arraylen(REMatch(replace(lcase(fileTypes),",","|","all"), lcase(valuelist(files.name))))>
 

<cfoutput>

filenames:#lcase(valuelist(files.name))#<br>

types: #replace(lcase(fileTypes),",","|","all")#<br>

<cfdump var="#REMatch(replace(lcase(fileTypes),",","|","all"), lcase(valuelist(files.name)))#" >
 

numret: #numret#

</cfoutput>

Open in new window

dgrafxtypes.jpg
0
 
LVL 63

Author Closing Comment

by:Zvonko
Comment Utility
Thanks dgrafx for sharing your nice solution.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now