Solved

writing a file

Posted on 2000-03-31
4
144 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now