Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write multiple lines to a file with single cffile statement? 2

Status
Not open for further replies.

LongHorn

Programmer
Jun 20, 2001
30
US
I want to use CFLOCK to guarantee that the file can be written by one user at a time.
OUTFILE is a static name, it is used in a macro therefore the file name can't be changed to be more random.
I want to write over an existing file, so the action must be WRITE, but that would overwrite what was written before by the current user.
But If I use APPEND, it would contain what was written by a different user.

Here's my code.

Thanks,
Longhorn

<CFSET MAX_LINES = 8>

<CFSET TXTOUTPUT1='#Trim(ContactInfo.first_name)# #Trim(ContactInfo.last_name)#'>
<CFSET TXTOUTPUT2='#Trim(ContactInfo.title)#'>
<CFSET TXTOUTPUT3='#Trim(ProjectInfo.company_name)#'>
<CFSET TXTOUTPUT4='#Trim(AddressInfo.addr_line1)#'>
<CFSET TXTOUTPUT5='#Trim(AddressInfo.addr_line2)#'>
<CFSET TXTOUTPUT6='#Trim(AddressInfo.city)#, TEXAS'>
<CFSET TXTOUTPUT7='#Trim(AddressInfo.zip)#-#Trim(AddressInfo.zip_ext)#'>
<CFSET TXTOUTPUT8='#Trim(ProjectInfo.proj_typ_txt)#'>

<cflock NAME=#OUTFILE# TIMEOUT=120 TYPE=&quot;Exclusive&quot;>
<CFLOOP INDEX=&quot;nLpIndx&quot; FROM=&quot;1&quot; TO=&quot;#MAX_LINES#&quot;>
<CFFILE ACTION=&quot;Append&quot;
FILE=#OUTFILE#
OUTPUT=#evaluate(&quot;TXTOUTPUT#nLpIndx#&quot;)#>
</CFLOOP>
</cflock>
 
I think you can just use a

<CFFILE ACTION=&quot;WRITE&quot; FILE=#OUTFILE# OUTPUT=&quot;&quot;>

prior to invoking your &quot;append&quot; action (between cflock and cfloop above). This way, the previous file will be deleted (overwritten with &quot;&quot;) and you can proceed with your append action to write the new version.

John Hoarty
jhoarty@quickestore.com
 
The easiest way to do this without using a tmp file (which can be a serious pain in the neck, because then you have to deal with OS as well) is to concatenate your output and do ONE SINGLE WRITE, instead of looping.

Instead of doing:
Code:
<cfset txtoutput1 =form.field1>
<cfset txtoutput2 =form.field2>
ect...
What you want to do is use the same variable for all your data, like so:

Code:
<cfset txtOutput = form.field1 & chr(10) & chr(13)>
<cfset txtOutput = txtOutput & form.field2 & chr(10) & chr(13)>
<cfset txtOutput = txtOutput & form.field3 & chr(10) & chr(13)>
ect....

Now write the file:
Code:
<CFFILE ACTION=&quot;Write&quot; FILE=#OUTFILE# OUTPUT=#TXTOUTPUT#>
viola, you have the data written all at once, only using one variable (which is easier on the server and faster to boot).

If you do not know, the chr(10) & chr(13) create a carrige return and new line.

Hope this help,

David Giffin
Davtri Interactive Design
 
Hi David,
Thank you very much. that was what I was hoping for. Do you know what is the max char I can assign to txtoutput?

LH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top