Solved

Writing Reading to a Text File

Posted on 2001-08-21
5
413 Views
Last Modified: 2007-11-27
Is it possible (and easy) to write to and read from a simple .txt file, or another similar file using ASP?  Any sample code appreciated!
0
Comment
Question by:2djohn
5 Comments
 
LVL 7

Accepted Solution

by:
John844 earned 200 total points
ID: 6411271

How do I write to a text file?

<%@ Language=VBScript %>
<%option explicit%>
<HTML>
<BODY>
<%
dim strFileName
strFileName = server.mappath("test.txt")

Const ForReading = 1, ForWriting = 2, ForAppending = 8
dim file
dim fso

set fso = server.CreateObject("Scripting.FileSystemObject")
set file = fso.OpenTextFile(strFileName,ForAppending ,true)
file.write "test" & vbCrLf
file.close

set file = nothing
set fso = nothing
%>
</BODY>
</HTML>
0
 
LVL 7

Expert Comment

by:John844
ID: 6411283
for reading a file

<%@ Language=VBScript %>
<%option explicit%>
<HTML>
<BODY>
<%
dim strFileName
strFileName = server.mappath("test.txt")


Const ForReading = 1, ForWriting = 2, ForAppending = 8
dim file
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
set file = fso.OpenTextFile(strFileName,ForAppending ,true)
file.write "test" & vbCrLf
file.close

set file = fso.OpenTextFile(strFileName,ForReading)
Response.Write replace(file.readall, vbCrLf, "<BR>")
file.close

set file = nothing
set fso = nothing

%>
</BODY>
</HTML>
0
 

Expert Comment

by:flopperman
ID: 6411292
Remember to make sure that the Internet Guest Account (or whatever account is used for the web-server) has the proper rights to the folder where you want to write the files.
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6412083
Here is some code and functions I use in VBscript, give them a try.

~Turbo


dim xFileType, xFileNum, xDocumentType, xDesc
Dim filename, overwrite, TextToPrint
Dim FileStreamObject, Write, TextOutput, PrintOutput
Dim BaseLocationForTextFiles
Const ForReading   = 1
Const ForWriting   = 2
Const ForAppending = 8

'BaseLocationForTextFiles = Left(Request.ServerVariables("PATH_TRANSLATED"),len(Request.ServerVariables("PATH_TRANSLATED"))-len(Request.ServerVariables("SCRIPT_NAME"))) & "\logfiles\"
BaseLocationForTextFiles = Trim(Server.MapPath ("\")) & "\logfiles\"

Sub CreateTextFile (filename,overwrite,TextToPrint)
     Set FileStreamObject = CreateObject("Scripting.FileSystemObject")
     If overwrite = "" Or overwrite = "0" Or overwrite = "FALSE" Then
          overwrite = "FALSE"
     Else
          overwrite = "TRUE"
     End If
     
     filename = BaseLocationForTextFiles & filename
     Set Write = FileStreamObject.CreateTextFile(filename, overwrite)
     Write.writeline(TextToPrint)
     Write.Close
     Set FileStreamObject = Nothing
     Set Write = Nothing
End Sub

Sub OpenTextFileForReading(filename)
     Set FileStreamObject = CreateObject("Scripting.FileSystemObject")
     filename = BaseLocationForTextFiles & filename
     Set TextOutput = FileStreamObject.OpenTextFile (filename,1,"FALSE")

     While not TextOutput.AtEndOfStream
          PrintOutput = TextOutput.ReadLine
           If not TextOutput.AtEndOfStream Then
                 TextOutput.SkipLine()
           End If
     Wend
     Set TextOutput = Nothing
     Set FileStreamObject = Nothing
End Sub

Sub DeleteTextFile(filename)
     Set FileStreamObject = CreateObject("Scripting.FileSystemObject")
     filename = BaseLocationForTextFiles & filename
     FileStreamObject.DeleteFile(filename)
     Set FileStreamObject = Nothing
End Sub
0
 

Author Comment

by:2djohn
ID: 6417076
Thanks for the suggestions everyone.  I gave John844 the points as he was first in, but I appreciate everyone's feeback.

2DJohn
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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.

776 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