Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

Writing Reading to a Text File

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
2djohn
Asked:
2djohn
1 Solution
 
John844Commented:

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
 
John844Commented:
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
 
floppermanCommented:
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
 
turbosigCommented:
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
 
2djohnAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now