DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   VB.NET (http://www.daniweb.com/forums/forum58.html)
-   -   Help with appending text file (http://www.daniweb.com/forums/thread85689.html)

tonyf Aug 8th, 2007 10:53 am
Help with appending text file
 
Hi

I'm using the following code to append a text file:

Dim sw As IO.TextWriter
sw = IO.File.AppendText("C:\MyTextFile.txt")
sw.WriteLine(text1.text)
sw.Flush()
sw.Close()

For some reason this is the output:

frh001wa2555655
f
fr
frh
frh0
frh00
frh001
frh001w
frh001wa
frh001wa2
frh001wa26
frh001wa261
frh001wa2613
frh001wa26135
frh001wa261351
frh001wa2613519

Please help!

Thanks

Tony

richerTextBox Aug 8th, 2007 12:21 pm
Re: Help with appending text file
 
Are you saying that you have a bunch of lines that added in succesion instead of one line?
Try using the StreamWriter class. I have used it on a number of occassions to write text to a file and I have had no problems.

Cheers!
Richard

tonyf Aug 8th, 2007 2:35 pm
Re: Help with appending text file
 
Hi Richard

Thanks for your help. I've tried using the StreamWriter class and it creates the file and writes to it with no problem. I haven't figured out how to append to it using the StreamWriter class. Using ohter methods as stated in my initial post, the outcome was as you've stated:
"Are you saying that you have a bunch of lines that added in succesion instead of one line?
"
Let me eplain to you what I need to do. I have an app that I put together to perform remote tasks. I want to be able to type in the host name in a textbox, save it to a text file, and append the text file every time I type in a new host name only if the host name doesn't already exist.

Regards,

Tony

QVeen72 Aug 8th, 2007 4:07 pm
Re: Help with appending text file
 
Hi,

OK, then u need to parse the whole text file..

Open the TextFile, read the Contents, and check if the Hotsname is already present... else add..
check this code:

Dim sw As System.IO.TextReader
sw = System.IO.File.OpenText("C:\MyTextFile.txt")
Dim MyContents As String = sw.ReadToEnd
sw.Close()
If Instr(MyContents,Text1.Text) = 0  Then
  ' Text does not contain the name
  sw = IO.File.AppendText("C:\MyTextFile.txt")
  sw.WriteLine(text1.text)
  sw.Flush()
  sw.Close()
 
End If

Regards
Veena

arjunsasidharan Aug 9th, 2007 4:04 am
Re: Help with appending text file
 
This should work.

Dim sw as StreamWriter
Try
  sw = File.AppendText(txtFileName.Text)
  sw.Write(txtFilename.Text)
  sw.Flush()
Catch exc As Exception
  Msgbox(exc.Message)

Finally
  If Not sw Is Nothing Then
                sw.Close()
  EndIf
EndTry

tonyf Aug 10th, 2007 10:33 am
Re: Help with appending text file
 
HI all

Thank you for your help. The coed that you've all provided me with works but for some reason my output to the text file is still the same as you will notice below:

frh001wa2555655
f
fr
frh
frh0
frh00
frh001
frh001w
frh001wa
frh001wa2
frh001wa26
frh001wa261
frh001wa2613
frh001wa26135
frh001wa261351
frh001wa2613519

Tony

QVeen72 Aug 12th, 2007 11:19 am
Re: Help with appending text file
 
Hi,

Have u tried my code..?

Regards
Veena

tonyf Aug 13th, 2007 10:39 pm
Re: Help with appending text file
 
Hi Veena

Unfortunately it didn't work I get the following error:
System.IO.streamwriter cannot be converted to System.IO.TextWriter.

Here's wahat i am trying to accomplish:

The code works fine for the first line. It's when I try to append the text file I get the funky output. Here is the actual code:

Dim sw As New System.IO.StreamWriter("D:\host.txt", True)
sw.WriteLine(Me.TextBox1.Text)
sw.Close()

What I'm trying to do is create the text file and append the file with machine names from textbox1.

the ideal output would be

frh001wa2555655
edi002ma2341234
etc...
etc...

Also,I would like to append with the host name only if the name doesn't already exist in the text file.

Thanks

Tony

tonyf Aug 15th, 2007 5:40 pm
Re: Help with appending text file
 
Hi Veena

It turns out that the code you've provided was helpful. I've just used the StreamReader class instead.

Thanks

Code:

Dim sw As StreamWriter
Dim sr As System.IO.StreamReader
If File.Exists("D:\HostFile.txt") Then
sr = System.IO.File.OpenText("D:\HostFile.txt")
Dim MyContents As String = sr.ReadToEnd
sr.Close()
If InStr(MyContents, t_HostName.Text) = 0 Then

sw = New StreamWriter("D:\Hostfile.txt", True) 'True for appending
sw.WriteLine(Me.t_HostName.Text)
sw.Flush()
sw.Close()
End If
Else

'Pass the file path and the file name to the StreamWriter constructor.
sw = New StreamWriter("D:\Hostfile.txt", True) 'True for appending
sw.WriteLine(Me.t_HostName.Text)
'Close the file.
sw.Flush()
sw.Close()
End If

QVeen72 Aug 17th, 2007 12:09 pm
Re: Help with appending text file
 
Hi,

I just didnt realise that.. Yes u will have to use StreamWriter, to write the file..

Any way, good, ur problem is solved..

Regards
Veena


All times are GMT -4. The time now is 1:05 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC