1,105,625 Community Members

Help with appending text file

Member Avatar
tonyf
Newbie Poster
20 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
richerTextBox
Newbie Poster
7 posts since Jul 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
tonyf
Newbie Poster
20 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
QVeen72
Veteran Poster
1,016 posts since Nov 2006
Reputation Points: 104 [?]
Q&As Helped to Solve: 164 [?]
Skill Endorsements: 7 [?]
 
0
 

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

Member Avatar
arjunsasidharan
Practically a Posting Shark
812 posts since Aug 2006
Reputation Points: 297 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
tonyf
Newbie Poster
20 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
QVeen72
Veteran Poster
1,016 posts since Nov 2006
Reputation Points: 104 [?]
Q&As Helped to Solve: 164 [?]
Skill Endorsements: 7 [?]
 
0
 

Hi,

Have u tried my code..?

Regards
Veena

Member Avatar
tonyf
Newbie Poster
20 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
tonyf
Newbie Poster
20 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
QVeen72
Veteran Poster
1,016 posts since Nov 2006
Reputation Points: 104 [?]
Q&As Helped to Solve: 164 [?]
Skill Endorsements: 7 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article