Solved

Writing Reading to a Text File

Posted on 2001-08-21
5
416 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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 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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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