Solved

saving info to a text file....

Posted on 2001-07-23
13
534 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

13 Experts available now in Live!

Get 1:1 Help Now