Replacing Value in .CSV file-VBForums
Replacing Value in .CSV file-VBForums
csv file-VBForums
Remember Me?
New Posts FAQ Calendar Forum Actions Quick Links Advanced Search
VBForums Visual Basic Visual Basic .NET Replacing value in .csv file
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start
viewing messages, select the forum that you want to visit from the selection below. Click
Here
Results 1 to 6 of 6 to
Expand
Thread: Replacing value in .csv file Forum
to Full
Width
Thread Tools Display
Train_Wreck
Replacing value in .csv file
Thread Starter
Junior Member Hey, i have a question: Is it possible to replace a value in a .csv file using VB????
kareninstructor
Re: Replacing value in .csv file
Karen Payne MVP
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.
Join Date: Jun 2008
Location: Oregon
Posts: 6,688
2014-Current
.paul.
Re: Replacing value in .csv file
eXtreme Programmer
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")
Join Date: May 2007 5. end if
Location: Chelmsford UK 6. next
Posts: 25,539 7.
8. io.file.writealllines("csv.txt", lines)
Coding Examples:
My Web Site | CodeBank Submissions
Features:
Sorting Techniques | DataGridView Printing | Average Strategies - Plugin Version
Online Games:
hangMan | masterMind | sudoku | crack the lock | numbers game | Fit Words | Fit Numbers | Crossword Hangman
Compiled Games:
Puzzle Games
https://www.vbforums.com/showthread.php?685013-Replacing-value-in-csv-file 1/3
16:05 13/06/2024 Replacing value in .csv file-VBForums
Train_Wreck
Re: Replacing value in .csv file
Thread Starter
Junior Member thanks for the reply i will check out the links and post something if/when i figure out how to write the code.
ThomasJohnsen
Re: Replacing value in .csv file
Fanatic Member
vb Code:
1. dim lines() as string = io.file.readalllines("csv.txt")
2. for x as integer = 0 to lines.getupperbound(0)
Join Date: Jul 2010
3. if lines(x).contains(".,") then
Location: Denmark 4. lines(x) = lines(x).replace(".", "Value4")
Posts: 528 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
IO.File.WriteAllLines("csv.txt", lines)
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)
Train_Wreck
Re: Replacing value in .csv file
Thread Starter
Junior Member
Originally Posted by .paul.
Join Date: Jun 2012
vb Code:
Posts: 24
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)
VBForums Visual Basic Visual Basic .NET Replacing value in .csv file
asp.net, vb.net
View Tag Cloud
Posting Permissions
Terms of Service | About Us | Privacy Notice | Contact Us | Advertise | California - Do Not Sell My Info
Copyright 2024 TechnologyAdvice. All Rights Reserved.
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact
how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products
available in the marketplace.
All times are GMT -5. The time now is 03:52 AM.
https://www.vbforums.com/showthread.php?685013-Replacing-value-in-csv-file 3/3