...

View Full Version : Vb-Write Multiple lines to a text file



Bry Man
12-29-2004, 08:03 AM
Hey,

I have a porgram that writes user entered info into a text file from a text box, it also writes each new submission on a new line, however I would like to expand that from writing one line at a time to multiple lines at once for more information like for example, a name one one line the persons phone number on the next...that sorta thing. Ive googled around and this is the closest thing I can find, the only problem is that it overwrites the info thats already there.


'VBScript
'Create a text file

DIM fso, GuyFile

Set fso = CreateObject("Scripting.FileSystemObject")
Set GuyFile = fso.CreateTextFile("e:\ezine\newsletter13.txt", True)
GuyFile.WriteLine("Ezine 13 Available Jan 11th")
GuyFile.Write ("Next week will be about creating user accounts")
GuyFile.Write (" More on same line")
GuyFile.Close

is there a way to modify the above script so that it writes after the information that was already entered?

shmoove
12-29-2004, 09:47 AM
Check out the documentation for fso.CreateTextFile() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsmthcreatetextfile.asp), the second parameter is overwrite, so if you set it to false it won't.

shmoove

Bry Man
12-29-2004, 07:42 PM
ah thanks :thumbsup:

EDIT: Hmm actually, that jsut doent overwrite the file istelf, when the file exists the program freaks out and dies. I want to be able to add additional information into the file without losing whats in there already

oracleguy
12-29-2004, 08:43 PM
Thats because you are using the CreateTextFile method. You need to use the OpenTextFile method if you want to append data.

Bry Man
12-29-2004, 10:03 PM
Hmm I tried that but it is giving me an error, here what I did, first I put the origonal code that I posed into a button, then I put the same code into another button only changed it to opentextfile instead of create. heres the code for the two buttons

Button 1:

'VBScript
'Create a text file

DIM fso, GuyFile

Set fso = CreateObject("Scripting.FileSystemObject")
Set GuyFile = fso.CreateTextFile("c:\test.txt", True)
GuyFile.WriteLine("Ezine 13 Available Jan 11th")
GuyFile.Write ("Next week will be about creating user accounts")
GuyFile.Write (" More on same line")
GuyFile.Close

Button 2:


Set fso = CreateObject("Scripting.FileSystemObject")
Set GuyFile = fso.OpenTextFile("c:\test.txt")
GuyFile.WriteLine("Ezine 13 Available Jan 11th")
GuyFile.Write ("Next week will be about creating user accounts")
GuyFile.Write (" More on same line")
GuyFile.Close

When i hit button 2 it gives me this error' run time error 54: Bad file mode' and then highlights this line in button 2


GuyFile.WriteLine("Ezine 13 Available Jan 11th")

oracleguy
12-30-2004, 08:08 AM
Try specifying the IOMode parameter on the OpenTextFile method. The details for it are on the MSDN. If you specify the mode that you will be appending, it will probably work because the default might be ForReading.

Basscyst
12-31-2004, 12:42 AM
Yep, and I would do this just in case the file isn't there. And rather than writing over and over, I would concatenate the strings and add in a \r\n for the line breaks as needed. That way you could easily prepare the string before passing it to the function.



<html>
<head>
<script type="text/vbscript">

Function txtLine(txt,fpath)

'VBScript
'Create or append a text file

DIM fso, flg, floc
Set fso = CreateObject("Scripting.FileSystemObject")
fpath="c:\test.txt"
flg=fso.FileExists(fpath)

If flg Then
Set floc= fso.OpenTextFile(fpath, 8)
floc.WriteLine(txt)
floc.Close
Else
Set floc = fso.CreateTextFile(fpath,true)
floc.WriteLine(txt)
floc.Close
End If

End Function

</script>
</head>
<body>
<form>
<input type="button" onclick='vbscript:txtLine "Its beddah with Cheddah From The Nebbah Kinezah","C:\Test.txt" ' value="Gibberish" />
</form>
</body>
</html>


Basscyst

Bry Man
12-31-2004, 01:06 AM
Ill look into that in a bit ive been a little busy with my web host, they are kinda givin me some troubles with my account and resetting it for me. Thanks Basscyst I was going to look into making a script that checked if the file exists and if not create one that should be quite useful. Whats with all the html in there tho?

Basscyst
12-31-2004, 01:08 AM
I added to it a bit, it's a bit more re-useable now. I just threw the HTML in so you could see that it was working.

Basscyst

Bry Man
12-31-2004, 01:15 AM
Cool, this is going to be used in a prog that keeps track of members in my clan for Kings Of Chaos because the leaders are having a time keeping track of all of us. So far I have it so you jsut type in a name into a text box and hit enter but id like to have a bit more than that like their name their link and their rank etc. Im also looking to add the ability to have more than one file, possibly by diming a variable for it and such which souldnt be very hard at all.

Basscyst
12-31-2004, 01:22 AM
You could just use a flat text db, and store the data that way. . .if I'm understanding correctly, you are just keeping track of people. . .

So you could setup your text file like this:

Name|Age|Rank|Location|Whatever
Adam|27|General|CA|Happy
Bob|99|Peon|WA|Sad

See where I'm going with this? That way you only need one file. It's a nice cheap and easy alternative to a database when you just have a bit of data. . . .well it is a database. . ;)

Basscyst

Brandoe85
12-31-2004, 01:23 AM
You could use a database to store all of this instead of a text file. (just a suggestion i'm sure you have a reason why your using a text file I just thought i'd throw that out there) :p

Roelf
12-31-2004, 09:40 AM
uhm, no need for all that code to check if the file exists.
opentextfile takesathird (boolean) argument which indicates the creation of the file if it doesn't exist


Set floc= fso.OpenTextFile(fpath, 8, True) will do the whole trick for you

Bry Man
01-01-2005, 05:58 AM
Sorry fot not repling my gmail notifyer...didnt notify me of new posts. Im using a text file because I load the text file intoa list, but now that think of it having more than one line per user being loaded nt the list may be a bit confusing unless I could find a way where I could click the users name and have it display their information somehow....im kinda getting ahead of myself but Im not sure when I can get to work on all this becuase I wrenched my back and cant sit at the computer for a long time...I have a program however that has three text boxes , one for your name email and password and each text box corresponds to its own colum in the list..only thing is I didnt make it so I dunno how it was done but I think that would be almost perfect.

chmpmps
03-17-2005, 10:01 AM
hi
i need a little help.
i have this
Private Sub write_to_file(ByVal fname As String, ByVal buf As String)
Dim FS
Dim s
Dim t
Set FS = CreateObject("Scripting.FileSystemObject")
Set s = FS.OpenTextFile(fname, 8, True)
s.WriteLine (buf)
s.Close
Set FS = Nothing
End Sub

but i want to write in a textbox instead of a text file!
how is it possible. any help would be useful



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum