扫描目录中的所有文件并按文件类型返回计数 [英] Scan all files in a directory and return counts by file type
问题描述
我刚刚加入了一家 IT 公司和开始与 ColdFusion 合作.我的经理希望我在 ColdFusion 中编写代码:
I have just joined an IT company & started working with ColdFusion. My Manager wants me to write code in ColdFusion which:
- 将扫描包含数百个文件(包括 jQuery 文件、cfm 文件等)的任何目录(例如
c:cf
)并给出这些文件的数量(我们可以手动选择文件类型显示).
- Will scan any directory (say
c:cf
) which contains hundreds of files including jQuery files, cfm files etc. and give the counts of such files (we can manually select which file type to show).
我写了这段代码:
<cfdirectory
action="list"
directory="direcoty path"
name="Files"
recurse = "yes"
filter="*.*" />
<cfoutput>No of Java Script: #files.recordCount#</cfoutput>
但它一次显示一种文件类型.如何同时检查多种文件类型?
but it shows one file type at a time. How can I check multiple file types at the same time?
推荐答案
对于第一个问题,有两种方法.循环遍历您感兴趣的每个不同文件类型,为每个文件创建一个 cfdirectory.
For the first question, there's two approaches. Either loop over each of the different file types you're interested in, doing a cfdirectory for each.
<cfset filetypes = arrayNew(1)>
<cfset arrayAppend(filetypes, "js")>
<cfset arrayAppend(filetypes, "cfm")>
<cfset arrayAppend(filetypes, "pdf")>
<cfloop index="i" from="1" to="#arrayLen(filetypes)#">
<cfdirectory
action="list"
directory="directory path"
name="Files"
recurse = "yes"
filter="*.#filetypes[i]#" />
<cfoutput>No of #filetypes[i]# files: #files.recordCount#<br></cfoutput>
</cfloop>
或者您可以在一个 CFDirectory 中处理多种文件类型,请参阅 http://www.bennadel.com/blog/1221-CFDirectory-Filtering-Uses-Pipe-Character-For-Multiple-Filters-Thanks-Steve-Withington-.htm
Or you can do multiple file types in one CFDirectory, see http://www.bennadel.com/blog/1221-CFDirectory-Filtering-Uses-Pipe-Character-For-Multiple-Filters-Thanks-Steve-Withington-.htm
<cfdirectory
action="list"
directory="directory path"
name="Files"
recurse = "yes"
filter="*.js|*.cfm|*.pdf" />
<cfoutput>No of JS/CFM/PDF files: #files.recordCount#</cfoutput>
这篇关于扫描目录中的所有文件并按文件类型返回计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!