Click to See Complete Forum and Search --> : Storing the contents of a text file within a variable


gregw74
09-01-2006, 11:04 AM
As a rookie in the world of ASP I was looking for a method to store the contents of text file within a variable. I am so far able to read a text file and output the results. Going a step further I also wanted to replace a particular string within those same results using the "Replace" function. This much works as well, but only when it is written to the screen. My question, how can I store the final result within a variable so that I can reference it later.

The working code I am using now, is as follows:

<%

Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("junk.txt"), 1)

do while f.AtEndOfStream = false
response.write(Replace(f.ReadLine, "________", "12345"))
Response.Write("<br>")
loop

f.Close

Set f=Nothing
Set fs=Nothing
%>

russell_g_1
09-03-2006, 08:13 AM
<%

Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("junk.txt"), 1)

'read everything out of the file
content = f.readall()

'replace the underscores
content = replace(content, "________", "12345")

'replace the carriage returns with html breaks (not sure if you really want this here or where it gets output).
content = replace(content,chr(13),"<br>")

f.Close

Set f=Nothing
Set fs=Nothing


response.write content

%>

gregw74
09-03-2006, 12:55 PM
This is perfect!! Thank you so much! I'd buy you a beer if I could :) I'm such a newbie, it's pathetic, I know. I basically wanted this so that the content that displays on the screen while the form is being filled out is the same content that gets pushed with with the email ....or if I use a Preview page the content doesn't have to be duplicated. Basically, I'll have just one text file that needs to be updated, no duplicating. I would think this is fairly common, new to me though. Of coarse what isn't in the realm of ASP. Thanks again! :D