Search code examples
coldfusioncoldfusion-8cffile

How to include ColdFusion code but not have it processed?


I am using ColdFusion 8.

I am creating some code that will create a folder and insert a file. The file will be tweaked later by a human.

I create the directory like this:

<cfdirectory action="create" directory="#LOCAL.PathToCreate#">

Next, I need to create a file with the name of index.cfm. Right now, the content that needs to go into the index.cfm file is within Test-21-index.txt. I create the content like this:

<cfsavecontent variable="LOCAL.MyContent">
    <cfinclude template="Test-21-index.txt">
</cfsavecontent>
<cffile action="write" file="#NewTreatmentPath##LOCAL.NewFile#" output="#LOCAL.MyContent#" nameconflict="overwrite">

The content of Test-21-index.txt has ColdFusion code in it. The problem that I am encountering is that when I include the file Test-21-index.txt, the ColdFusion code is being run.

How do I get the code to be created as a text file that is NOT run during the creation of the file?

UPDATE ~ With so many tools available to implement my solution, I just needed the right combination. Instead of reading the file, using cfsavecontent, and then writing the file, I merely copied the file. Ugh. That was way too easy.

<cffile action="copy" source="#CurrentDirectory#\#LOCAL.FileToInclude#" destination="#NewTreatmentPath#\#LOCAL.NewFile#">

Solution

  • Rather than using cfinclude, you can use cffile to read the file and then output its contents in the cfsavecontent tag:

    <cffile action="read" file="test-21-index.txt" variable="fileContent" />
    
    <cfsavecontent variable="LOCAL.MyContent">
       <cfoutput>#filecontent#</cfoutput>
    </cfsavecontent>
    
    <!--- or even <cfset LOCAL.MyContent = fileContent /> --->
    
    <cffile action="write" file="#NewTreatmentPath##LOCAL.NewFile#" output="#LOCAL.MyContent#" nameconflict="overwrite">
    

    Code is untested, but I think it should work. I believe you will need to work out the path to test-21-index.txt a little differently, though.