扫描目录中的所有文件并按文件类型返回计数 [英] Scan all files in a directory and return counts by file type

查看:10
本文介绍了扫描目录中的所有文件并按文件类型返回计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚加入了一家 IT 公司和开始与 ColdFusion 合作.我的经理希望我在 ColdFusion 中编写代码:

I have just joined an IT company & started working with ColdFusion. My Manager wants me to write code in ColdFusion which:

  1. 将扫描包含数百个文件(包括 jQuery 文件、cfm 文件等)的任何目录(例如 c:cf)并给出这些文件的数量(我们可以手动选择文件类型显示).
  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆