Substituição de strings dentro de todos os arquivos em um diretório
Problema:
Voce quer substituir/deletar todas as ocorrências de uma particular string dentro de todos os arquivos com uma certa extensão dentro de um diretório.
Solução:
Use a funçëo "replace" junto com as tags "cffile" e "cfdirectory".
Explicação detalhada:
<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>
by Thazleem
Versão em inglês: Adobe ColdFusion Cookbook
0 responses to “Substituição de strings dentro de todos os arquivos em um diretório”