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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing a text file? 1

Status
Not open for further replies.

Laurelin

Programmer
Jan 24, 2002
29
0
0
US
I am trying to take a text file that has around 7000 lines of data and make a few small edits to it so that the data can be moved to a database. Basically I am trying to take a date/time field that is written as "12/10/09 1158" and make it look like "12/10/09 11:58:00" because the database will not recognize the date/time field if imported under it's current format.

I would like to be able to edit the whole file at once but so far I've had no luck. I have been trying to use the StreamReader & StreamWriter classes to do this but I'm not having any luck. Writing the data to a new file would be a pretty good idea for me as well, that way I can just pull out the fields of data that I need moved to the database table.

If anybody has any tips or code examples to do this I sure would appreciate it.

I am currently trying something like this but it is not writing every record into my new file. I'm only getting around 2200 of the 6800 records.(Please don't laugh too hard, this is my first shot at writing anything) :-(

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fileReader As StreamReader
        Dim fileWriter As StreamWriter
        Dim results As DialogResult
        Dim fileout As DialogResult
        Dim trans_date As String
 
        results = OpenFileDialog1.ShowDialog
        fileout = SaveFileDialog1.ShowDialog
 
            If results = DialogResult.OK Then
            fileReader = New StreamReader(OpenFileDialog1.FileName)
            fileWriter = New StreamWriter(SaveFileDialog1.FileName, False)
 
            While Not fileReader.EndOfStream
                trans_date = fileReader.ReadLine.Substring(461, 8) + fileReader.ReadLine.Substring(469, 3) + ":" + fileReader.ReadLine.Substring(472, 2) + ":00" & ControlChars.NewLine
                
                fileWriter.Write(trans_date)
                
            End While
 
            fileReader.Close()
            fileWriter.Close()
        Else
            
        End If
 
    End Sub

My output file is coming out with the data that I want but it is just way too short of records. I don't understand why all the records are not being edited and moved over.

Output file data:
12/07/09 20:50:00
12/08/09 07:02:00
12/08/09 08:03:00
12/08/09 08:53:00
12/08/09 08:01:00

Appreciate any help with this.
 

I believe this is the problem:

Code:
trans_date = fileReader.ReadLine.Substring(461, 8) + fileReader.ReadLine.Substring(469, 3) + ":" + fileReader.ReadLine.Substring(472, 2) + ":00" & ControlChars.NewLine

Every time you reference fileReader.ReadLine it reads a new line. To work on one line at a time, read it into a string variable then do the changes:

Code:
Dim ThisLine As String

ThisLine = fileReader.ReadLine

trans_date = ThisLine.Substring(461, 8) + ThisLine.Substring(469, 3) + ":" + ThisLine.Substring(472, 2) + ":00" & ControlChars.NewLine



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you jebenson. You are exactly right, I am getting the right amount of records into my new file now.

Much appreciated. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top