VB.net Find And Replace from Data in a DataGridView in a text file

vbvirg20

Im sure someone out there can help, im totally new to coding but getting into it and really enjoying. I know this is such a simple question out there for you folks but i have the following, I load a spread sheet of strings (2 columns) into a datagridview the reason i do this because there is over 100,000 find and replaces and these will generally sit within and existing string when searching, then from there i want to simply search a txt file and find and replace a number of strings in it. So it would check each row in a datagrid take from column 1 the find and use column 2 to replace then outputs the string to another txt file once the find and replace has taken place. My current results are that it just takes what was in the first file and copies without replacing in the second find.

Any assistance is gratefully received, many thanks.

Please see below my amateur code:-

Private Sub CmdBtnTestReplace_Click(sender As System.Object, e As System.EventArgs) Handles CmdBtnTestReplace.Click
    Dim fName As String = "c:\backup\logs\masterUser.txt"
    Dim wrtFile As String = "c:\backup\logs\masterUserFormatted.txt"
    Dim strRead As New System.IO.StreamReader(fName)
    Dim strWrite As New System.IO.StreamWriter(wrtFile)
    Dim s As String
    Dim o As String


    For Each row As DataGridViewRow In DataGridView1.Rows
        If Not row.IsNewRow Then

            Dim Find1 As String = row.Cells(0).Value.ToString
            Dim Replace1 As String = row.Cells(1).Value.ToString

            Cursor.Current = Cursors.WaitCursor
            s = strRead.ReadToEnd()

            o = s.Replace(Find1, Replace1)

            strWrite.Write(o)

        End If

    Next

    strRead.Close()
    strWrite.Close()

    Cursor.Current = Cursors.Default
    MessageBox.Show("Finished Replacing")

End Sub
Karl Stephen

1. What you are doing is :

  • creating a StreamReader whose purpose is to read chars from a File/Stream in sequence.
  • creating a StreamWriter whose purpose is to add content to a File/Stream.
  • then looping
    a) read the remaining content of file fName and put it in s
    b) replace words from s and put the result in o
    c) add o to the existing content of the file wrtFile
  • then the usual closing of the stream reader/writer...

But that doesn't work because, on the secund iteration of the loop, strRead is already at the end of your loaded file, then there is nothing to read anymore, and s is always an empty string starting from the secund iteration.

Furthermore, because s is empty, o will be empty aswell.

And last of all, even if you manage to re-read the content of the file and replace the words, strWrite will not clear the initial content of the output file, but will write the resulting replaced string (o) after the previously updated content of the file.


2. Since you loaded the content of the file in a string (s = strRead.ReadToEnd()), why don't you :

  • load that s string before the For-Next block
  • loop the datagridview rows in a For-Next block
  • replace using the pair Find1/Replace1 s = s.Replace(Find1, Replace1)
  • then, save the content of s in the targeted file outside the For-Next block

3. However, improving your understanding of how streams work, what should be considered and what are forbidden is a bit outside the scope of SO I think; such documentation could be found/gathered on the MSDN page or with the help of your friend : google. The same applies for finding out/thinking of how you should arrange your code, how to achieve your goal.

Let's take an example :

' Content of your file :
One Two Three Four Five Six

' Content of your DataGridView :
One | Two
Two | Three
Three | Four
Four | Five
Five | Six
Six | Seven

The resulting replacement text at the end of a similar routine as yours will be :

Seven Seven Seven Seven Seven Seven ' :/ 
' while the expected result would be :
Two Three Four Five Six Seven

And that's because of the iteration : already replaced portions of your file (or loaded file content) could get replaced again and again. To avoid that, either :

  • split the loaded content in single words, and use a "replaced" flag for each word (to avoid replacing that word more than once)
  • or preload all the pair Find/Replace, and parse the file content in sequence once, replacing that instance when required.

So, before using an interesting object in the framework :

  • you should know what it does and how it behaves
  • otherwise -> read the documentation
  • otherwise -> create a minimalistic test solution which purpose is to brute force testings on that particular object to debunck all its powers and flaws.

So, like I said in 2., move those ReadAllText() and Write() outside the For/Next block to start from and have a look at the resulting output (Ask specific questions in comments when google can't answer) Then if you're OK with it even if issue like the One Two Three example above could occur, then voila ! Otherwise, use google to gather more examples on "splitting text in words" and reformating the whole, have some tries, then get back here if you're stuck on precise issues.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

from DataGridView saving to text file issue when saving date values on vb.net

From Dev

How to export the content of a datagridview to a text file vb.net 2010

From Dev

Formatting text output from DataGridView in vb.net

From Dev

How to Fill Data From Database to datagridview and combobox - vb.net

From Dev

Problem in Data display from database using datagridview in vb.net

From Dev

How to Fill Data From Database to datagridview and combobox - vb.net

From Dev

How to pass data from a DataGridView to another VB.NET

From Dev

Drawing a graph from DataGridView data vb.net

From Dev

Exporting data from DataGridView as Excel table using VB.NET

From Dev

Batch to FIND and REPLACE with inputs from a text file

From Dev

VB.NET Find Duplicate Lines in a Text File

From Dev

Displaying SQLite Data In DataGridView VB.net

From Dev

Displaying SQLite Data In DataGridView VB.net

From Dev

Find and replace lines in text file with output from another file

From Dev

VB.Net Text File vs Database for storing data?

From Dev

VB.net .tostring("0.000") from datagridview

From Dev

Remove a line from text file vb.net

From Dev

Read lines from a text file in VB.NET

From Dev

how to remove duplicate line from text file vb.net

From Dev

Vb.net text to speech using voice from file?

From Dev

How to get line number from a text file in vb.net

From Dev

Open a text file scrolled at the end from vb.net

From Java

Find and Replace Inside a Text File from a Bash Command

From Dev

How to Use Load Data .dat from Matlab in VB.NET with DataGridView

From Dev

VB.NET: Cannot display selected data from access database into datagridview

From Dev

Find and Replace a character in a string in VB.NET

From Dev

Find and replace text file Matlab

From Dev

Find and replace text file Matlab

From Dev

VB.Net: Getting File Path from Data Files In Project

Related Related

  1. 1

    from DataGridView saving to text file issue when saving date values on vb.net

  2. 2

    How to export the content of a datagridview to a text file vb.net 2010

  3. 3

    Formatting text output from DataGridView in vb.net

  4. 4

    How to Fill Data From Database to datagridview and combobox - vb.net

  5. 5

    Problem in Data display from database using datagridview in vb.net

  6. 6

    How to Fill Data From Database to datagridview and combobox - vb.net

  7. 7

    How to pass data from a DataGridView to another VB.NET

  8. 8

    Drawing a graph from DataGridView data vb.net

  9. 9

    Exporting data from DataGridView as Excel table using VB.NET

  10. 10

    Batch to FIND and REPLACE with inputs from a text file

  11. 11

    VB.NET Find Duplicate Lines in a Text File

  12. 12

    Displaying SQLite Data In DataGridView VB.net

  13. 13

    Displaying SQLite Data In DataGridView VB.net

  14. 14

    Find and replace lines in text file with output from another file

  15. 15

    VB.Net Text File vs Database for storing data?

  16. 16

    VB.net .tostring("0.000") from datagridview

  17. 17

    Remove a line from text file vb.net

  18. 18

    Read lines from a text file in VB.NET

  19. 19

    how to remove duplicate line from text file vb.net

  20. 20

    Vb.net text to speech using voice from file?

  21. 21

    How to get line number from a text file in vb.net

  22. 22

    Open a text file scrolled at the end from vb.net

  23. 23

    Find and Replace Inside a Text File from a Bash Command

  24. 24

    How to Use Load Data .dat from Matlab in VB.NET with DataGridView

  25. 25

    VB.NET: Cannot display selected data from access database into datagridview

  26. 26

    Find and Replace a character in a string in VB.NET

  27. 27

    Find and replace text file Matlab

  28. 28

    Find and replace text file Matlab

  29. 29

    VB.Net: Getting File Path from Data Files In Project

HotTag

Archive