Results 1 to 6 of 6

Thread: Replacing value in .csv file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    24

    Replacing value in .csv file

    Hey, i have a question: Is it possible to replace a value in a .csv file using VB????

    Here is an example of what im talking about:

    Column1, Column2, Column3, Column4, Column5
    Value1, Value2, Value3, ., Vlaue5

    i need to replace ,., with , Value4,

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Replacing value in .csv file

    You would read the file in say with TextFieldParser class line by line, place the data into a string array then write the data back to the file after finishing looping thru the file.

    The replacement happens as you are reading a line that meets critiera to change text. Use IO.File.WriteAllLines to write the array back to disk.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Replacing value in .csv file

    vb Code:
    1. dim lines() as string = io.file.readalllines("csv.txt")
    2. for x as integer = 0 to lines.getupperbound(0)
    3.     if lines(x).contains(".,") then
    4.         lines(x) = lines(x).replace(".", "Value4")
    5.     end if
    6. next
    7.  
    8. io.file.writealllines("csv.txt", lines)

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    24

    Re: Replacing value in .csv file

    thanks for the reply i will check out the links and post something if/when i figure out how to write the code.

  5. #5
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Replacing value in .csv file

    Quote Originally Posted by .paul. View Post
    vb Code:
    1. dim lines() as string = io.file.readalllines("csv.txt")
    2. for x as integer = 0 to lines.getupperbound(0)
    3.     if lines(x).contains(".,") then
    4.         lines(x) = lines(x).replace(".", "Value4")
    5.     end if
    6. next
    7.  
    8. io.file.writealllines("csv.txt", lines)
    This bit of code may not work as intended. Assuming any of value1, value2, value3 or value5 contain a ".", you would end up replacing more "."s than the one we're interested in replacing. Using indexof would most likely be a better option, depending on the input data ofc.

    #EDIT: The following should work regardless of contents of the other fields (at least on the small sample of data I tried it on):
    Code:
            Dim lines() As String = IO.File.ReadAllLines("csv.txt")
            Dim i As Integer
    
            For x As Integer = 0 To lines.GetUpperBound(0)
                i = lines(x).IndexOf(".,")
                If i > 0 Then
                    lines(x) = lines(x).Substring(0, i) & "value4" & lines(x).Substring(i + 1, lines(x).Length - i - 1)
                End If
            Next
    
            IO.File.WriteAllLines("csv.txt", lines)
    Last edited by ThomasJohnsen; Jul 25th, 2012 at 10:39 AM.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    24

    Re: Replacing value in .csv file

    Quote Originally Posted by .paul. View Post
    vb Code:
    1. dim lines() as string = io.file.readalllines("csv.txt")
    2. for x as integer = 0 to lines.getupperbound(0)
    3.     if lines(x).contains(".,") then
    4.         lines(x) = lines(x).replace(".", "Value4")
    5.     end if
    6. next
    7.  
    8. io.file.writealllines("csv.txt", lines)
    hey this worked perfectly!!!!!!! Thankz

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
  •  



Click Here to Expand Forum to Full Width