Solved

saving info to a text file....

Posted on 2001-07-23
13
537 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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
 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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