cf_searchFiles

cf_searchFiles

Version: 1.1 build 20041229
Requires: Adobe ColdFusion 5.0 or greater
Total size: 3.30 KB
Download time: 0.59 seconds at 56kbps
Edition: Freeware

Description

Perform a recursive search of a folder and sub-folders, listing matches for one or more search terms. Supports case-sensitive/insensitive search, and regular expression or plain search. This tag could be modified to perform any action recursively across folders and files.

Result

Results dumped to screen

Category

search

Parameters


Parameter Type Required? Default Description
folder string Yes Absolute path of folder to search
keys list or array Yes Search terms as a comma-delimited list or an array
flags string No String of characters enabling settings: c: case sensitive search (default insensitive) r: regular expression search (default non-regex) o: Find one match for each key per file (default all)

Custom tag source


 
<!---**
* cf_searchfiles
* Performs a recursive search of a folder and sub-folders, listing matches
* for one or more search terms. Supports case-sensitive/insensitive search, and 
* regular expression or plain search. This tag could be modified to perform
* any action recursively across folders and files.
*
* @param folder             Absolute path of folder to search (required)
* @param keys               Search terms - either an array or a comma-
*                           delimited list (required)
* @param flags              string of characters enabling settings:
*                           c: case sensitive search (default insensitive)
*                           r: regular expression search (default non-regex)
*                           o: Find one match for each key per file (default all)                         
* @result Dumps results out to screen
* @author Matthew Walker, WWW.eswsoftware.com
* @version 1.1, 20041229
* Recent changes: 
* 20041229 * bugfix, vwersion 1.1 released
* 20040703 * version 1 released
*--->

<cfparam name="attributes.folder" type="string">
<cfparam name="attributes.keys">
<cfparam name="attributes.flags" default="">
<cfparam name="request.eswsoftware" default="#structNew()#">
<cfparam name="request.eswsoftware.searchfiles" default="#structNew()#">
<cfset request.eswsoftware.searchfiles.version = 1.1>

<cfif not isArray(attributes.keys)>
  <cfset attributes.keys = listToArray(attributes.keys)>
</cfif> 

<cfset caseSensitive = findNoCase("c", attributes.flags)>
<cfset regex = findNoCase("r", attributes.flags)>
<cfset oneMatch = findNoCase("o", attributes.flags)>

<ul>
  <li>Scanning folder <cfoutput>#attributes.folder#</cfoutput></li>
  <cfdirectory action="LIST" directory="#attributes.folder#" name="folder">
  
  <cfoutput query="folder">
    <cfif type eq "dir">
      <cfif name neq "." and name neq "..">
        <cf_searchfiles folder="#attributes.folder##name#\" keys="#attributes.keys#" flags="#attributes.flags#">
      </cfif>
    <cfelse>
      <cffile action="READ" file="#attributes.folder##name#" variable="theFile">  
      <cfloop index="i" from="1" to="#arrayLen(attributes.keys)#">
        <cfset key = attributes.keys[i]>
        <cfset notDone = true>
        <cfset startPos = 1>
        <cfloop condition="#notDone#">
          <cfif caseSensitive>
            <cfif regex>
              <cfset foundPos = reFind(key, theFile, startPos)>
            <cfelse>
              <cfset foundPos = find(key, theFile, startPos)>
            </cfif>
          <cfelse>
            <cfif regex>
              <cfset foundPos = reFindNoCase(key, theFile, startPos)>
            <cfelse>
              <cfset foundPos = findNoCase(key, theFile, startPos)>
            </cfif>
          </cfif>
          <cfif foundPos>
            <!--- count lines --->
            <cfset chunk = left(theFile, foundPos)>
            <cfset line = listLen(chunk, "#chr(13)##chr(10)#")>
            <li>Found "<strong>#key#</strong>" in <strong>#attributes.folder##name#</strong> line <strong>#line#</strong></li>
            <cfset startPos = foundPos + 1>
            <cfset notDone = oneMatch>  
          <cfelse>
            <cfset notDone = false>  
          </cfif>
        </cfloop>
      </cfloop>
    </cfif>
  </cfoutput>
</ul>
World Wide Web