5 Replies - 3929 Views - Last Post: 17 May 2012 - 08:34 AM Rate Topic: -----

#1 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

How to separate each column in a textfile?

Posted 17 May 2012 - 12:36 AM

Hi,

I have a .txt file, with five columns - each column is seperated by a space " "

What I'm trying to do is,
1, Add column headers and
2, align the columns to make them look presentable.

For the first part all I know is how to append lines to a text file, my problem is that whenever I append to the textfile it always gets appended to the bottom.

Using SW As New IO.StreamWriter("d:\temp\test.txt", True)
		SW.WriteLine("The Headers Should Go Here")
	End Using



How would I append to the top of the textfile?

Is This A Good Question/Topic? 0
  • +

Replies To: How to separate each column in a textfile?

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5468
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: How to separate each column in a textfile?

Posted 17 May 2012 - 03:10 AM

Quote

each column is seperated by a space " "

If its an option I strongly recommend changing that to be a comma or tab.
Comma-delimited and tab-delimited text files are very standard for this type of need. But a space is such a commonly used character that its a bad choice.


Quote

my problem is that whenever I append to the textfile it always gets appended to the bottom.

That's what append does. If you want the new data someplace else in the file you'll have to write a new file in the order you want the data. A text file does not allow inserting: You have to write from start to end.


Quote

How would I append to the top of the textfile?

You don't. You have to organize your data in memory, then write a new file.
Was This Post Helpful? 0
  • +
  • -

#3 pradeep1210  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 56
  • Joined: 08-May 12

Re: How to separate each column in a textfile?

Posted 17 May 2012 - 05:32 AM

Try this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '' your header - separate the columns with comma.
    Dim header As String = "My,Sexy,Header Line,Goes,Here"

    '' the numbers are the maximum width of the fields (starting first column to last one)
    FixFileData("C:\Temp\test.txt", "C:\Temp\test-1.txt", header, 10, 10, 15, 20, 10, 10, 10, 10, 15, 10)
    MessageBox.Show("Done!")
End Sub

Private Sub FixFileData(ByVal inputFileName As String, ByVal outputFileName As String, ByVal header As String, ByVal ParamArray fieldWidths() As Integer)
    Dim fileData() As String = IO.File.ReadAllLines(inputFileName)
    For row = 0 To fileData.Length - 1
        Dim fields = Split(fileData(row), " ")
        For i = 0 To fields.Length - 1
            fields(i) = fields(i).PadRight(fieldWidths(i))
        Next
        fileData(row) = Join(fields, vbTab)
    Next

    Using sw As New IO.StreamWriter(outputFileName)
        '' write header
        Dim fields = Split(header, ",")
        For i = 0 To fields.Length - 1
            fields(i) = fields(i).PadRight(fieldWidths(i))
        Next
        header = Join(fields, vbTab)
        sw.WriteLine(header)

        '' write data rows
        For Each row In fileData
            sw.WriteLine(row)
        Next
        sw.Close()
    End Using
End Sub


Was This Post Helpful? 0
  • +
  • -

#4 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: How to separate each column in a textfile?

Posted 17 May 2012 - 06:24 AM

Thanks Pradeep,

Just a quick question what would I need to do to just add the header to a file and not worry about the alignment?

I have the following

Dim header As String = "My,Sexy,Header Line,Goes,Here"

        FixFileData("D:\Catlist.txt", "D:\Catlist2.txt", header, 10, 10, 15, 20, 10, 10, 10, 10, 15, 10)
        MessageBox.Show("Done!")

Dim fileData() As String = IO.File.ReadAllLines(inputFileName)

        Using sw As New IO.StreamWriter(outputFileName)
            '' write header
            Dim fields = Split(header, ",")
            For i = 0 To fields.Length - 1
                fields(i) = fields(i).PadRight(fieldWidths(i))
            Next
            header = Join(fields, vbTab)
            sw.WriteLine(header)
            '' write data rows
            sw.Close()




Is this correct? many thanks
Was This Post Helpful? 0
  • +
  • -

#5 pradeep1210  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 56
  • Joined: 08-May 12

Re: How to separate each column in a textfile?

Posted 17 May 2012 - 07:49 AM

View Postvnewbie, on 17 May 2012 - 06:54 PM, said:

Thanks Pradeep,

Just a quick question what would I need to do to just add the header to a file and not worry about the alignment?


Most part of that code was being used to format the data. Since now u no longer need to format the records, you can insert your header row simply like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '' your header - format it whatever way u want it to look like.
    Dim header As String = "My Sexy Header Line Goes Here"

    '' add header to file
    AddHeaderToFile("C:\Temp\test.txt", "C:\Temp\test-1.txt", header)
    MessageBox.Show("Done!")
End Sub

Private Sub AddHeaderToFile(ByVal inputFileName As String, ByVal outputFileName As String, ByVal header As String)
    Dim fileData As String = IO.File.ReadAllText(inputFileName)
    Using sw As New IO.StreamWriter(outputFileName)
        sw.WriteLine(header)          '' write header
        sw.Write(fileData)            '' write data rows
        sw.Close()
    End Using
End Sub


This post has been edited by pradeep1210: 17 May 2012 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: How to separate each column in a textfile?

Posted 17 May 2012 - 08:34 AM

Perfect
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1