扫描目录中的所有文件,并按文件类型返回计数 [英] 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:
- 将扫描任何目录(例如
c:\cf \
),它包含数百个文件,包括jQuery文件,cfm文件等,并给出这些文件的计数(我们可以手动选择显示哪种文件类型)。
- 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-MultipleFilters-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屋!