• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 552
  • Last Modified:

saving info to a text file....

Ok here's my problem. Below is some information that I need to save into a text file called "orders.txt"
Below is an examples of one order. In this text file,
orders.txt, there will be many orders but all with the same format as the example below. Also the orders will not be saved to the text file all at once (if this matters..) It will just be a text file that whenever an order is placed has the order information placed into it in the below format separated by a few spaces from the last order. Do I use <cfupdate..>? i thought that was only for updating fields in a database? is there some sort of a <cfwrite...> command?
--------------------------------------
Dealer Name; Ford Shipping Systems;
Dealer Number; 337;
P.O Number; 2352564565;
Special Shipping Instructions; Please send 2nd day air UPS;
Date; 2001-07-20 18:20:17;
Product ID1; cb1; quantity; 23;
Product ID2; cb2 quantity; 12;
Product ID3; cb3 quantity; 17;
.
.
.
Product IDN; cb2 quantity; X;
----------------------------------------
0
Judy Deo
Asked:
Judy Deo
  • 9
  • 3
1 Solution
 
CF_SpikeCommented:
You can do this as follows:

<CFSET fullpath = "D:\sites\mysite\txtfile.txt">
<CFSET texttoappend = "sometext">
<CFFILE ACTION="APPEND"
        FILE="#fullpath#"
        OUTPUT="#texttoappend#">

In order to make sure that 2 concurrent users don't cause a problem you should wrap the cffile tag in a cflock tag:

<CFSET fullpath = "D:\sites\mysite\txtfile.txt">
<CFSET texttoappend = "sometext">
<CFLOCK TYPE="Exclusive" Name="txtfileaccess" Timeout="2">
<CFFILE ACTION="APPEND"
        FILE="#fullpath#"
        OUTPUT="#texttoappend#">
</CFLOCK>

Every time you read from this file you also need to use a cflock tag, with the type attribute set to "readonly" and the other 2 attributes with the same values.

Spike
0
 
Judy DeoAuthor Commented:
<CFSET fullpath = "D:\sites\mysite\txtfile.txt">

for the above line, if my .cfm file is in the same directory as this text file, txtfile.txt,
do i change the line to:

<CFSET fullpath = "txtfile.txt">   ??

i tried this (first i created txtfile.txt and uploaded it into the same directory as the .cfm file).
but nothing was inserted into this file.

just as a test, i changed the value of the fullpath
variable to "txtfile22.txt" expecting the page to give
me an error since that file does not exist. instead
i got no error at all...





0
 
Judy DeoAuthor Commented:
i printed out the values of the two variables,
fullpath & texttoappend. they print out fine.

i also put two "I am here" test lines before and
after the <cffile...> tag and they both printed out fine.

So is this <cffile...> just now doing anything?
i can't understand why if i change the name of the file
to one that doesn't exist, why an error doesn't show up?!


0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Judy DeoAuthor Commented:
i printed out the values of the two variables,
fullpath & texttoappend. they print out fine.

i also put two "I am here" test lines before and
after the <cffile...> tag and they both printed out fine.

So is this <cffile...> just now doing anything?
i can't understand why if i change the name of the file
to one that doesn't exist, why an error doesn't show up?!


0
 
Judy DeoAuthor Commented:
i also tried setting the value of fullpath to
the full path, i.e http://www.mydomain.com/txtfile.txt
where the .cfm file is in the same directory.


<CFSET fullpath = "http://www.mydomain.com/txtfile.txt">   
0
 
Judy DeoAuthor Commented:
this is the error i get when i set the fullpath variable to the full address:

Error: All or part of the path is invalid.  

-----------------------------
i also tried setting the value of fullpath to
the full path, i.e http://www.mydomain.com/txtfile.txt
where the .cfm file is in the same directory.


<CFSET fullpath = "http://www.mydomain.com/txtfile.txt">
-----------------------------

0
 
Judy DeoAuthor Commented:
that was not the complete error message:

Error attempting to write data to target file 'http://www.mydomain.com/txtfile.txt'.

Error: All or part of the path is invalid.  
0
 
CF_SpikeCommented:
If the text file is in the same directory as your .cfm files you must still specify a full path.

If you don't know the full path you can get it for the currently executing template using getBaseTemplatePath().

The following code will create the correct path for the cffile tag if the txt file is in the same directory as the .cfm files:


<CFSET Fullpath = GetBaseTemplatePath()>
<CFSET path = Replace(FullPath,getFileFromPath(Fullpath),'')>
<CFSET txtpath = path & 'txtfile.txt'>
<CFSET texttoappend = "sometext">
<CFLOCK TYPE="Exclusive" Name="txtfileaccess" Timeout="2">
<CFFILE ACTION="APPEND"
       FILE="#txtpath#"
       OUTPUT="#texttoappend#">
</CFLOCK>

Spike
0
 
cheekycjCommented:
Use this:

write.cfm
<!---
     Author: CJ Jouhal
     Date: Sometime in 2000
     Version: 1.0
     
     Data will be written to a text file.
     you can change the path to where you want the file to go too.
     
     Usage:
     <CF_WRITE Data="data to write" FileName="filename">
 --->

<CFIF IsDefined("ATTRIBUTES.Data") AND  IsDefined("ATTRIBUTES.FileName")>
     <CFSET VARIABLES.Data = ATTRIBUTES.Data>
      <CFSET VARIABLES.FileName = ATTRIBUTES.FileName>
     <CFSET thisPath= ExpandPath("*.*")>
     <CFSET thisDirectory= GetDirectoryFromPath(thisPath)>
     <CFLOCK TIMEOUT="60">
          <CFIF FileExists("#thisDirectory#/#VARIABLES.CounterName#.txt")>
               <CFFILE ACTION="APPEND" FILE="#thisDirectory##VARIABLES.FileName#.txt" OUTPUT="#VARIABLES.Data#" ADDNEWLINE="Yes">
          <CFELSE>
               <CFFILE ACTION="WRITE" FILE="#thisDirectory##VARIABLES.FileName#.txt" OUTPUT="#VARIABLES.Data#" ADDNEWLINE="Yes">
          </CFIF>
     </CFLOCK>
<CFELSE>
     <FONT COLOR="Red"><B>ERROR: You must provide data and file name</B></FONT>
</CFIF>


===========================================================
Copy this file (Write.cfm) to the same directory as your file that is calling it.

Now you can use the custom tag by putting the following code in your file:
<CF_Write Data="#orderDataVariable#" FileName="txtfile">

HTH,
CJ
0
 
Judy DeoAuthor Commented:
CF_Spike,
okay the information is finally stored in the text file.
last thing i need to do is include the contents of another file (extrainfo.cfm) after the text in the the variable
'texttoappend'.
in other words, i need the contents of my variable
'texttoappend' to be:

------------------------------
customer name
customer address
customer phone

the content of extrainfo.cfm
-----------------------------

can i just put a <cfinclude template="extrainfo.cfm">
statment inside the 'texttoappend' cfset statment?

i.e
<CFSET texttoappend = "#name# #address# #phone#
<cfinclude template="extrainfo.cfm"> ">

the #name#, #address#, #phone# values work fine...just
the <cfinclude...> part doesn't work..


before i was using <cfinclude template="extrainfo.cfm">
when i was just including the contents in a regular .cfm page.

**i've included an extra 20 points for this question
since i didn't state this in the original question**

---------------------------------------
<CFSET Fullpath = GetBaseTemplatePath()>
<CFSET path = Replace(FullPath,getFileFromPath(Fullpath),'')>
<CFSET txtpath = path & 'txtfile.txt'>
<CFSET texttoappend = "sometext">
<CFLOCK TYPE="Exclusive" Name="txtfileaccess" Timeout="2">
<CFFILE ACTION="APPEND"
      FILE="#txtpath#"
      OUTPUT="#texttoappend#">
</CFLOCK>
---------------------------------------
0
 
CF_SpikeCommented:
If the cfinclude is creating the data then you will have to turn that into a variable which you can append to the variable.

i.e. in extrainfo.cfm you would do something like this:

<CFSET txtfiledata = "">
logic code...
<CFSET txtfiledata = txtfiledata & "Customer Name">
logic code ...
<CFSET txtfiledata = txtfiledata & "John Smith">
logic code ...
<CFSET txtfiledata = txtfiledata & "Customer Address">
logic code ...
<CFSET txtfiiledata = txtfiledata & "10 Alewife center">

etc.

After the cfinclude you will have a variable called txtfile data which you can put into the text file.

Spike
0
 
Judy DeoAuthor Commented:
it worked! thanks.
0
 
Judy DeoAuthor Commented:
very complete answer. solved my problem 100%
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now