Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Count files in one directory filtered by a set of filetypes

Posted on 2008-10-03
9
Medium Priority
?
949 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 63

Author Comment

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

Expert Comment

by:daviddriv
ID: 22637707
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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
LVL 63

Author Comment

by:Zvonko
ID: 22637786
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
 
LVL 14

Expert Comment

by:Scott Bennett
ID: 22638274
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
ID: 22638335
OK, then sorry again I have not limited the cfdirectory to one call. But that is the requirement.
0
 
LVL 25

Accepted Solution

by:
dgrafx earned 2000 total points
ID: 22638550
#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
ID: 22639621
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
ID: 31502912
Thanks dgrafx for sharing your nice solution.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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