Coldfusion 匹配目录中所有文件中的字符串并返回其总计数

Coldfusion 匹配目录中所有文件中的字符串并返回其总计数,coldfusion,coldfusion-7,Coldfusion,Coldfusion 7,我需要一种方法来搜索所有文件中不出现任何特定字符串的总数。例如,所有文件中出现“ABC”的总计数。之前,我有一个代码可以一次在单个文件上执行此操作: <cffile action="read" file="full_Path\file.txt" variable="filecontent"> <cfset charList = "strings to match/search"> <cfoutput>

我需要一种方法来搜索所有文件中不出现任何特定字符串的总数。例如,所有文件中出现“ABC”的总计数。之前,我有一个代码可以一次在单个文件上执行此操作:

    <cffile action="read"
        file="full_Path\file.txt"
        variable="filecontent">
    <cfset charList = "strings to match/search">
    <cfoutput> 
    <cfloop list="#charList#" index="x"> 
        <cfset charCount = val(len(filecontent) - len(replace(filecontent,x,"","all")))/Len(x)> 
        Count of '#htmlEditFormat(x)#' = #charCount#<br> 
    </cfloop>
    </cfoutput>
如何将此输出格式化为

Name of File    Size    count of CFQuery    count of CFIF     count of CFElse  etc

好的,那么您要做的是循环遍历从cfdirectory调用获得的所有文件。您可能需要在某些逻辑中构建,以仅检查特定的文件类型(或者cfdirectory中的filter属性可以涵盖这一点)

在计算可能字符串列表的出现次数时,我们需要为每个字符串设置多个计数器。有多种方法可以将这些信息存储在变量中,我建议使用结构。因此,如果您要查找字符串“foo”和“bar”的计数,我建议您将得到如下结构:

{
    'foo' = 100,
    'bar' = 77
}
下面是我该怎么做。我首先为您搜索的每个字符串填充零,然后在文件上循环时递增它。我假设您计算搜索词实例数的代码是好的,我没有仔细研究它

<cfset charList = "foo,bar">
<cfset filetypes = arrayNew(1)>
<cfset arrayAppend(filetypes, "js")>
<cfset arrayAppend(filetypes, "cfm")>

<cfset stringCounts = structNew()>

<cfloop list="#charList#" index="x"> 
    <cfset stringCounts[x] = 0>
</cfloop>

<cfloop index="i" from="1" to="#arrayLen(filetypes)#">
    <cfdirectory
        action="list"
        directory="your directory" 
        name="Files"
        recurse = "yes"
        filter="*.#filetypes[i]#" />         

    <cfloop query="Files">
        <cffile action="read"
            file="#Files.directory#\#Files.name#"
            variable="filecontent">

        <cfloop list="#charList#" index="x"> 
            <cfset stringCounts[x] = stringCounts[x] + val(len(filecontent) - len(replace(filecontent,x,"","all")))/Len(x)> 
            <cfoutput>#Files.directory#\#Files.name# : count of '#htmlEditFormat(x)#' = #stringCounts[x]#<br></cfoutput>
        </cfloop>
    </cfloop>
</cfloop>

<cfloop collection="#stringCounts#" item="x"> 
    <cfoutput>Count of '#htmlEditFormat(x)#' = #stringCounts[x]#<br></cfoutput>
</cfloop>

#Files.directory\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
“#htmlEditFormat(x)#”的计数=#stringCounts[x]#

你检查过directoryList函数或cfdirectory标记吗?PS:如果有人想知道为什么我要费心按文件类型循环而不是只做一个cfdirectory,这与SKChauhan的一个同事有关,他在那里做这件事,并想将其与计算文件中字符串的代码结合起来。非常感谢duncan!我对代码做了一些修改,使其在CF7上工作。现在开始工作了。还将代码合并为一个!:)
{
    'foo' = 100,
    'bar' = 77
}
<cfset charList = "foo,bar">
<cfset filetypes = arrayNew(1)>
<cfset arrayAppend(filetypes, "js")>
<cfset arrayAppend(filetypes, "cfm")>

<cfset stringCounts = structNew()>

<cfloop list="#charList#" index="x"> 
    <cfset stringCounts[x] = 0>
</cfloop>

<cfloop index="i" from="1" to="#arrayLen(filetypes)#">
    <cfdirectory
        action="list"
        directory="your directory" 
        name="Files"
        recurse = "yes"
        filter="*.#filetypes[i]#" />         

    <cfloop query="Files">
        <cffile action="read"
            file="#Files.directory#\#Files.name#"
            variable="filecontent">

        <cfloop list="#charList#" index="x"> 
            <cfset stringCounts[x] = stringCounts[x] + val(len(filecontent) - len(replace(filecontent,x,"","all")))/Len(x)> 
            <cfoutput>#Files.directory#\#Files.name# : count of '#htmlEditFormat(x)#' = #stringCounts[x]#<br></cfoutput>
        </cfloop>
    </cfloop>
</cfloop>

<cfloop collection="#stringCounts#" item="x"> 
    <cfoutput>Count of '#htmlEditFormat(x)#' = #stringCounts[x]#<br></cfoutput>
</cfloop>