VS 2010 [RESOLVED] File read CSV format and write new file with one word in one line-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] File read CSV format and write new file with one word in one line

  1. #1

    Thread Starter
    Lively Member darkbb's Avatar
    Join Date
    Jan 2016
    Posts
    89

    Resolved [RESOLVED] File read CSV format and write new file with one word in one line

    I bumped into another difficulty.. I read the file (like CSV format) and, trying to write it in a single line. [Tried using For Loop , but, as i use Test1 lines.length, the output file only shows upto the old length]
    for example :

    Test1.txt contains : My output ends up only with : Output file Testresult.txt should be :
    abc/wzed|
    adc/wssf/ssed|
    sdkf/effd/qefdc/efsfd|
    efds/dfsd|
    abc|
    wzed|

    adc|
    wssf|
    ssed|

    sdkf/effd/qefdc/efsfd|
    efds/dfsd|
    abc|
    wzed|
    adc|
    wssf|
    ssed|
    sdkf|
    effd|
    qefdc|
    efsfd|
    efds|
    dfsd|

    Following is the part of coding... Please help...
    and.. i know that i can replace everything with "/" or "|" but, this is the format i'm looking for... some character will be coming after "|"... So, it must contain split & "|" in the end...

    Code:
    lines = IO.File.ReadAllLines(TextBox1.Text)
    For icnt As Integer = 0 To lines.Length - 1
      Dim lncnt As Integer
      If lines(icnt).Contains("|") Then
        Dim strq As String() = lines(icnt).Split("|")
        If strq(0).Contains("/") Then
          Dim spl1 As String() = strq(0).Split("/")
          For jcnt As Integer = 0 To spl1.Length - 1
            Dim strtmp As String = spl1(jcnt) & "|" & strq(1)
            lncnt = icnt + jcnt
            Array.Resize(lines, lines.Length + 1)
            For counter = lines.Length - 2 To lncnt Step -1
              lines(counter + 1) = lines(counter)
            Next
            lines(lncnt) = strtmp
          Next
          lines(lncnt + 1) = ""
          icnt = lncnt
          lncnt = 0
        Else
        End If
      Else
      End If
     Next
     IO.File.WriteAllLines(TextBox2.Text, lines)
    *Textbox1 & textbox2 get the location of the file...
    Last edited by darkbb; Aug 27th, 2016 at 02:05 AM. Reason: forgot to add a detail..

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,117

    Re: File read CSV format and write new file with one word in one line

    You say that there will be some characters coming after the "|" but you don't show any and your output doesn't seem to account for any. What you need to do is actually describe EXACTLY what the rules are. One example is all well and good but if it doesn't demonstrate what to do in every possible case then it's really not too much use. We need ALL the rules.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member darkbb's Avatar
    Join Date
    Jan 2016
    Posts
    89

    Re: File read CSV format and write new file with one word in one line

    Quote Originally Posted by jmcilhinney View Post
    You say that there will be some characters coming after the "|" but you don't show any and your output doesn't seem to account for any. What you need to do is actually describe EXACTLY what the rules are. One example is all well and good but if it doesn't demonstrate what to do in every possible case then it's really not too much use. We need ALL the rules.
    Very Sorry... The output should look like the following...

    The example ::

    Test1.txt contains : Output file Testresult.txt should be :
    abc/wzed|cad
    adc/wssf/ssed|sde
    efds/dfsd|cfv
    abc|cad
    wzed|cad
    adc|sde
    wssf|sde
    ssed|sde
    efds|cfv
    dfsd|cfv

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,246

    Re: File read CSV format and write new file with one word in one line

    Quote Originally Posted by darkbb View Post
    and.. i know that i can replace everything with "/" or "|" but, this is the format i'm looking for... some character will be coming after "|"... So, it must contain split & "|" in the end...
    Maybe something like?...

    Code:
    Dim lines = TextBox1.Lines
    
    Dim results As New List(Of String)
    For Each line In lines
        Dim items = line.Split({"/", "|"}, StringSplitOptions.RemoveEmptyEntries)
        For Each item In items
            results.Add(item & "|")
        Next
    Next
    
    Dim output As String = String.Join(Environment.NewLine, results)
    Quote Originally Posted by darkbb View Post
    Very Sorry... The output should look like the following...
    Too late.
    Last edited by Edgemeal; Aug 27th, 2016 at 02:56 AM.

  5. #5

    Thread Starter
    Lively Member darkbb's Avatar
    Join Date
    Jan 2016
    Posts
    89

    Re: File read CSV format and write new file with one word in one line

    Quote Originally Posted by Edgemeal View Post
    Maybe something like?...

    Code:
    Dim lines = TextBox1.Lines
    
    Dim results As New List(Of String)
    For Each line In lines
        Dim items = line.Split({"/", "|"}, StringSplitOptions.RemoveEmptyEntries)
        For Each item In items
            results.Add(item & "|")
        Next
    Next
    
    Dim output As String = String.Join(Environment.NewLine, results)
    It throws a error at

    Code:
    Dim output As String = String.Join(Environment.NewLine, results)
    Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to '1-dimensional array of String'

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,246

    Re: File read CSV format and write new file with one word in one line

    Quote Originally Posted by darkbb View Post
    Very Sorry... The output should look like the following...
    One more try,

    Code:
    Dim lines = TextBox1.Lines
    
    Dim results As New List(Of String)
    For Each line In lines
        If line.Length > 0 Then
            Dim opt = line.Split("|"c)(1)
            line = line.Replace("|" & opt, "")
            Dim items = line.Split("/"c)
            For Each item In items
                results.Add(item & "|" & opt)
            Next
        End If
    Next
    Dim output As String = String.Join(Environment.NewLine, results)

  7. #7

    Thread Starter
    Lively Member darkbb's Avatar
    Join Date
    Jan 2016
    Posts
    89

    Thumbs up Re: File read CSV format and write new file with one word in one line

    Quote Originally Posted by Edgemeal View Post
    One more try,

    Code:
    Dim lines = TextBox1.Lines
    
    Dim results As New List(Of String)
    For Each line In lines
        If line.Length > 0 Then
            Dim opt = line.Split("|"c)(1)
            line = line.Replace("|" & opt, "")
            Dim items = line.Split("/"c)
            For Each item In items
                results.Add(item & "|" & opt)
            Next
        End If
    Next
    Dim output As String = String.Join(Environment.NewLine, results)
    Your code still says the same error as i mentioned in the comment #5...
    So, I did some small changes to your code... and, it worked like charm...

    Thank you so much edgemeal...

    and, jmcilhinney - thanx for pointing out... otherwise, i would have not noticed mistake in my thread...

    Code:
    lines = IO.File.ReadAllLines(TextBox1.Text)
    Dim results As New List(Of String)
      For Each line In lines
        If line.Length > 0 Then
          Dim opt = line.Split("|"c)(1)
          line = line.Replace("|" & opt, "")
          Dim items = line.Split("/"c)
          For Each item In items
            results.Add(item & "|" & opt)
          Next
        End If
      Next
    lines = results.ToArray
    IO.File.WriteAllLines(TextBox2.Text, lines)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.