Solved

Writing Reading to a Text File

Posted on 2001-08-21
5
411 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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