Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  Text Files: Reading Them off Server
     [next Lesson]  Text Files: Meyers-Briggs parsing #1

Text File Writing by Charles Carroll

The following code snippets adds several lines to an ASCII file.

   filename=/learn/test/txtwrite.asp

<Test Script Below>


<html><head>
<TITLE>txtwrite.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
whichFN=server.mappath("/upload/tests/tempfile.txt")

' first, create the file out of thin air
Set fstemp = server.CreateObject("Scripting.FileSystemObject")
Set filetemp = fstemp.CreateTextFile(whichFN, true)
' true  = file can be over-written if it exists
' false = file CANNOT be over-written if it exists

filetemp.WriteLine("This is a brand new file!!!!")
filetemp.writeblanklines(3) 
filetemp.WriteLine("This is the last line of the file we created!")
filetemp.Close

' Now open it and add some lines
forappending =8
set filetemp=fstemp.OpentextFile(whichFN, forappending)
filetemp.writeline "a line we added later"
filetemp.writeline "another line we added later..."
filetemp.close

set filetemp=nothing
set fstemp=nothing

If err.number=0 then
   response.write "File was appended sucessfully!"
else
   response.write "VBScript Errors Occured!<br>"
   response.write "Error Number=#<b>" & err.number & "</b><br>"
   response.write "Error Desc. =<b>" & err.description & "</b><br>"
   response.write "Help Path =<b>" & err.helppath & "</b><br>"
   response.write "Native Error=<b>" & err.nativeerror & "</b><br>"
   response.write "Error Source =<b>" & err.source & "</b><br>"
   response.write "SQL State=#<b>" & err.sqlstate & "</b><br>"
end if


%>
</body></html>

This code displays the file we created:

   filename=/learn/test/txtwritedisplay.asp

<Test Script Below>


<html><head>
<TITLE>txtwritedisplay.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
whichname="tempfile.txt"
whichdir=Server.Mappath ("/upload/tests/")
whichFN=whichdir & whichname
forreading=1

Set fs = CreateObject("Scripting.FileSystemObject")
Set thisfile = fs.OpenTextFile(whichFN, forreading, False)
counter=0
do while not thisfile.AtEndOfStream
    counter=counter+1
    thisline=thisfile.readline
    response.write thisline & "<br>"
loop
thisfile.Close

set thisfile=nothing
set fs=nothing

%>
</body></html>

There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.