I am using ColdFusion 8.
I am trying to write this code block in CFSCRIPT, but can’t quite get it.
<cfloop query="Q">
<cfscript>
// CREATE NEW LINE
NewLine = "";
NewLine = NewLine & Q.product_url;
</cfscript>
<cffile action="append" file="#ThisFile#" output="#NewLine#">
</cfloop>
Here is the CFSCRIPT
// LOOP THROUGH QUERY RESULTS
for (i = 1; i lte Q.RecordCount; i=i+1) {
// CREATE NEW LINE
NewLine = "";
NewLine = NewLine & Q.product_url[i];
// READ THE FILE
File = fileOpen(ThisFile, "read");
// WRITE NEW LINE TO FILE
fileWriteLine(File, "#NewLine#");
fileWrite(ThisFile, File);
fileClose(File);
}
I know I am supposed to open the file, modify it, then close the file. I think I need to do that with each new line added.
What’s wrong with this code?
Fixed:
Issues addressed:
You don’t open and close the file each line; you a) open file, b) write 1-x lines, then c) close the file (alternately, you can write it all at once with
fileWrite())You don’t need both fileWriteLine and fileWrite, fileWriteLine is for line-by-line writing, while fileWrite is for writing and entire set of data in one shot.
I also changed the “write” to “append”, in case the file you’re writing to doesn’t exist upon the first execution–obviously, if you can guarantee your destination file exists, you can replace “append” with “write”. Keep in mind that changing this will also grow your file over time; it may not be what you want, so switch it back to “write” if this is the case.
Although I can’t be certain without seeing your exact error, I have a hunch it was
FileWrite()line that was the culprit.