Not yet rated

Problem

You want to replace/delete all occurrences of a particular string within all files or files with a certain extension within a directory.

Solution

Use the replace function along with cffile and cfdirectory tags.

Detailed explanation

<cfset testDir = "C:\Documents and
Settings\god\Desktop\test">
<!--- The folder within which search & replace operation
should be performed. --->
<cfdirectory action="list" directory="#testDir#"
name="testDirectory" filter="*.*" recurse="true">
<!--- set filter value to a particular extension to search only
within files with a certain extension. Set recurse
        to false if you dont want to include sub directories in
search --->

<cfloop query="testDirectory">
    <cfset filepath = "#testDir#\#testDirectory.Name#">
    <cffile action="Read" file="#filepath#"
variable="tempFile">
    <cfset oldString = "cat">
    <cfset newString = "dog">
    <cfset tempFile = #ReplaceNoCase(tempFile, oldString,
newString, 'all')#>
    <!--- Use replace in place of replacenocase to make the
search case specific. To replace only the first occurence of
    the string, set scope to one instead of all     --->
    <cffile action="write" file="#filepath#"
output="#tempFile#">
</cfloop>

Thazleem Ali :: Software Engineer :: www.toobler.com :: India


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes