coldfusioncoldfusion-7

Scan all files in a directory and return counts by file type


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

  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).

I wrote this code:

    <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?


Solution

  • 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>
    

    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>