Click to See Complete Forum and Search --> : File writing, looping, File reading


Lizo
09-06-2003, 11:58 AM
Hi all,

Im a complete noob to ASP, I only started looking at it last week so sorry for any stupid questions.

Basically I have webpage where you type something in a text area and press submit. I want to have the text saved in a .txt file. Ive managed this so far, but what I would like to do now is change the filename using a loop. So, for example, the first time you click submit it will be saved as news1.txt, second time would be news2.txt etc...

Then after doing this, on the homepage of the site, I would like it to read in the latest txt file. So the one with the highest number basically. I have managed to read in a text file but Im just not sure how to loop through things in ASP.

Thanks,
-Lizo.

David Harrison
09-06-2003, 12:29 PM
I'm kind of in the same situation as you but I think that you could do something like this:

Application("newsnum")=1

And then when you create the file name do it like this:

Dim filename="news"&Application("newsnum")&".txt"
Application("newsnum")=Application("newsnum")+1

Then when you want to display the last news item do this:

Dim dispfilename="news"&(Application("newsnum")-1)&".txt"

I'm reasonably sure that that would work however like I said, I'm in the same boat as you.

Lizo
09-06-2003, 02:36 PM
Thanks for that.

Ive been scouring the web for examples and came across something that I thought could help, so I botched it together with some sample scripts from w3schools and ive got it working lol.

Heres my code that Ive got if your interested,


dim news
news = Request.Form("news")

dim FSO
dim Folder
dim FileList
dim Count

set FSO = Server.CreateObject("Scripting.FileSystemObject")
set Folder = FSO.GetFolder(Server.MapPath("../news/"))
set FileList = Folder.files
Count = FileList.count

dim no
no = Count + 1

dim File

set File = FSO.CreateTextFile(Server.MapPath("../news/news" & no & ".txt"))
File.WriteLine(news)
File.Close

set Folder = nothing
set FileList = nothing
set Count = nothing
set File = nothing
set FSO = nothing



thats what Ive got for saving the file, and this is the bit that reads it back in...


dim FSO
dim Folder
dim FileList
dim Count
dim no
dim File

set FSO = Server.CreateObject("Scripting.FileSystemObject")
set Folder = FSO.GetFolder(Server.MapPath("../news/"))
set FileList = Folder.files
Count = FileList.count
no = Count

Set File=FSO.OpenTextFile(Server.MapPath("../news/news" & no & ".txt"),1)
Response.Write(File.ReadAll)
File.Close

set Folder = nothing
set FileList = nothing
set Count = nothing
set File = nothing
set FSO = nothing



Ive just realised that there is a major flaw with this method.

If you have say, news1, news2, news3, and you delete news2, instead of the new one being saved as news4 it will be saved as news3 because the count will be 2.

Im not sure if that makes sense, but basically it screws up if you delete a file. Im not sure how to get round this.

Anyway, thanks for getting back, I really should do more research before I post heh ;) .

-Lizo

txmail
09-06-2003, 11:22 PM
I have not worked with the file system object because I mostly deal with databases (an autoincriment data type would do this) and this would be really simple. But it seems like if you were to keep one file with the current count in it you could set a "pointer" of the most current file, and when saving the file you could read the file with the counter in it, each time you save a new text file you could incriment the file and then update it. I would really look a a database though, this is cake with database access.

Lizo
09-07-2003, 11:46 AM
Thanks for that txmail, never thought of having the count in a file. Ill definately use that idea.

Heh, databases scare me ;)

-Lizo