Solved

saving info to a text file....

Posted on 2001-07-23
13
535 Views
Last Modified: 2013-12-24
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
Comment
Question by:sagardeo
  • 9
  • 3
13 Comments
 
LVL 4

Expert Comment

by:CF_Spike
ID: 6308721
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
 

Author Comment

by:sagardeo
ID: 6308920
<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
 

Author Comment

by:sagardeo
ID: 6308999
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
 

Author Comment

by:sagardeo
ID: 6309011
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
 

Author Comment

by:sagardeo
ID: 6309047
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
 

Author Comment

by:sagardeo
ID: 6309053
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
Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

 

Author Comment

by:sagardeo
ID: 6309061
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
 
LVL 4

Expert Comment

by:CF_Spike
ID: 6309154
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 6309162
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
 

Author Comment

by:sagardeo
ID: 6309356
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
 
LVL 4

Accepted Solution

by:
CF_Spike earned 120 total points
ID: 6309403
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
 

Author Comment

by:sagardeo
ID: 6309510
it worked! thanks.
0
 

Author Comment

by:sagardeo
ID: 6309513
very complete answer. solved my problem 100%
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

23 Experts available now in Live!

Get 1:1 Help Now