Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Writing Reading to a Text File

Posted on 2001-08-21
5
Medium Priority
?
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Accepted Solution

by:
John844 earned 800 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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