Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

saving info to a text file....

Posted on 2001-07-23
13
Medium Priority
?
549 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:Judy Deo
[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
  • 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:Judy Deo
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:Judy Deo
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
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 

Author Comment

by:Judy Deo
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:Judy Deo
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:Judy Deo
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
 

Author Comment

by:Judy Deo
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:Judy Deo
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 480 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:Judy Deo
ID: 6309510
it worked! thanks.
0
 

Author Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

636 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