Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't get append to a text file working

Status
Not open for further replies.

sergeiY

Programmer
Feb 13, 2003
132
0
0
AU
Hi

I am trying to debug my app and I want to spit some info to a text file (append) but for some reason this Sub I wrote doesn't work... Can you see what am I doing wrong ?

Code:
Sub DumpToFile2(strOut)
'______________________
'*START* Write to file 
'______________________
	Const ForReading = 1, ForWriting = 2, ForAppending = 8
	Dim fso, f, ReadAllTextFile
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set f = fso.OpenTextFile(Server.MapPath("ErrorDump2.txt"), ForAppending, True)
	ReadAllTextFile = f.ReadAll
	f.Write ReadAllTextFile & strOut & "tt"
	f.Close
'____________________
'*END* Write to file 
'____________________
End Sub

Thank you

Sergei
 
Hey, I'm actually working on this type of thing at the moment, I can't see anything wrong at the moment really except that the line

Code:
Set fso = CreateObject("Scripting.FileSystemObject")

Should be

Set fso = Server.CreateObject("Scripting.FileSystemObject")

This post is best viewed at 1024 x 768
 
I changed it but it still doesn't work... and the funny thing I took this bit of code from VB Script help file [sadeyes]
 
I just ran this and it works, it doesn't use the textstream though, let me know if it helps.

Code:
Sub DumpToFile2(strOut)
	Const ForReading = 1, ForWriting = 2, ForAppending = 8
	Dim fso, f
	Set fso = Server.CreateObject("Scripting.FileSystemObject")
	Set f = fso.OpenTextFile(Server.MapPath("ErrorDump2.txt"),8,True)
	f.WriteLine(strOut & "tt")
	f.Close
	Set f = Nothing
	Set fso = Nothing
End Sub

This post is best viewed at 1024 x 768
 
Gatchaman,

Thank you it worked ! So what's was wrong ? I was doing exactly the same thing except I was also reading all from file ...

Sergei
 
by doing the read all you've already accessed the file in a different format than it was opened in, being that for appending, and by reading it you're causing it to revert to openforreading, hence locking out the ability to write, kind of like writing response values without a response.buffer, then redirecting
 
Yeah, I just realised that it's almost the same, I wrote that from scratch (Ignore my comment from the last post about the textstream, I forgot to re-read your code, I thought that's what you wre using).

I don't know what the problem is with the original code. However, I believe your original code would've done this:
ErrorDump2.txt -

Code:
Some Errortt

Then you get another error, it's added and it would've been

Code:
Some ErrorttSome ErrorttAnother Errortt

As you can see, when you did the read all, and then the f.write with readall, it would've basically appended the whole file to itself (doubling it's size) and then appended strOut.

Anyway, as a side note, I just ran your code locally and got this error:

Code:
Error Type:
Microsoft VBScript runtime (0x800A0036)
Bad file mode

And it was pointing to the line

Code:
ReadAllTextFile = f.ReadAll

So i guess that's where the problem was, don't know what to do about it though, all I could think would be that for some reason you can't read from a file and assign the result to a variable, but that would seem silly.

This post is best viewed at 1024 x 768
 
Ah, ne'rmind I just realised what DreXor posted while I was writing my post. duh.

This post is best viewed at 1024 x 768
 
Ok. Thank you guys.

Now I understand what I did wrong.

I didn't know that calling a method I can change file opening format... intresting...

Sergei
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top