Solved

writing a file

Posted on 2000-03-31
4
149 Views
Last Modified: 2013-12-24
I need to write some code which will allow a client to query a database and then write the results of the query into a text file (csv) to be placed in the same folder as the database on the server.

The query is easy, but how do I write the text file.

Thanks for the help.
0
Comment
Question by:RICHARDH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:bdemarzo
ID: 2674444
Use CFFILE to write to the text file.

<CFFILE ACTION=WRITE FILE="filename" OUTPUT="output text">

You first need to create the output text, best done in a variable. Let's say you query a database as follows:

<cfquery name="myquery" ...>
SELECT field1, field2
FROM mydbf
</cfquery>

Then create the variable with your CSV text:

<cfoutput query="myquery">
<cfset myoutput=myquery.field1 & "," & myquery.field2>
</cfoutput>

Finally, add the CSV file to your text file:

<cffile action="write" file="c:\myfile.csv" output=#myoutput#>

That should do it! Note that if you want to APPEND to a text file rather than overwriting it, use action="append" in the CFFILE tag.
0
 

Accepted Solution

by:
bdemarzo earned 50 total points
ID: 2674446
And of course, that's my final answer, Regis...
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 2674473
basically do this:

<cfset filename = "/pathtodb/text.csv">
<cfloop query="queryname">
<cfset Result= "#queryname.field1#" & "," "#queryname.field2#" & "," "#queryname.field3#">
<cffile action="APPEND" file="#filename#" output="#Result#" addnewline="Yes">
</cfloop>

If you don't know the column names or field names then do this:
<cfloop index="i" list="queryname.columnlist" delimiters=",">
<cfset Result = "#Result#" & "#queryname[index]#" & ","
</cfloop>
chop of the last ","

I am not sure if the queryname[index]
is right but I can find out the exact syntax if you need it.

CJ
0
 

Author Comment

by:RICHARDH
ID: 2674565
Thanks to both people for the answers. they seem quite different, will they both do the same thing.

thanks to both of you.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question