Reading .txt files from a list box, removing a set of words.

g00dknight

New member
Joined
Feb 20, 2011
Messages
3
Programming Experience
Beginner
Hey everyone,

I'm currently writing an application but I've hit a wall.
Currently, my application will send the contents of a directory into a list box.

VB.NET:
Dim folderDlg As New FolderBrowserDialog
        folderDlg.ShowNewFolderButton = True
        If (folderDlg.ShowDialog() = DialogResult.OK) Then
            ListBox1.Text = folderDlg.SelectedPath
            Dim di As New IO.DirectoryInfo(folderDlg.SelectedPath)
            Dim myFileInfoArray As IO.FileInfo() = di.GetFiles()
            ListBox1.ValueMember = "FullName"
            ListBox1.DataSource = myFileInfoArray
            ListBox1.DisplayMember = "Name"
        End If

For now, all that is inside the directory is a text file.
I would like my program to read the contents of the text file and remove a set of common words, but I don't really know how to move forward here.

Forgive me for I am a beginner, but would it be best to create a class that holds a set of words that I would like to remove, then loop through the text file, remove any words that match those inside my class and then write what's left to a new .txt file?

Could someone lend a hand here?

Thanks in advance! :cool:
 
Hiya,

Yes, that sounds like it would work. I'm not sure if you need a full class though, you should be able to use a List or Array, since you would only need to store one value (the words to remove).

I would tend to read in each file and use the String.Replace method to replace all the words that match with an empty string...

Example:
VB.NET:
' The words to replace. Note space before and after word to ensure an entire word is matched.
        ' So Basic matches, but Basics would not.
        Dim wordsToReplace() As String = {" test ", " Visual ", " Basic "}

        Using sr As System.IO.StreamReader = New System.IO.StreamReader("C:\test.txt")

            ' Read the entire file into memory.
            Dim str As String = sr.ReadToEnd()

            ' Replace all the words with nothing.
            For Each s As String In wordsToReplace
                str = str.Replace(s, " ")
            Next

            ' Write the new file to a different location. Note different filename since C:\test.txt is still open.
            Using sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\test1.txt")
                sw.Write(str)
[INDENT][INDENT]sw.Close()
[/INDENT][/INDENT]End Using

            sr.Close()
        End Using
This just illustrates reading a text file from a hard-coded path, removing "test", "Visual" and "Basic" and writing the file to a new file, similar to you described. It should be enough to get you going, but post back if you need more help.

The using statements ensure the files get closed when we're finished reading to and writing to them.
 
You are the best, what can I say!
Thanks that's really helped and it's certainly given me a few ideas :D

What I would like to do now is rather than process files from a static directory, I would like to process the .txt files that populate my listbox, and then re-populate my listbox with the processed files.

So currently I have a form with two buttons and a listbox, the first button will populate my listbox (as seen in my code snippet in the first post), and when I click the second button I want to process those files, and re-populate the listbox.

Is it possible to modify your code by simply pointing to the listbox, or is this going to be ridiculously hard?

(how many times can I say listbox in one post?)

Thanks
 
You can't directly point to the listbox, since it's not a string. But you can loop through each Item in the listbox, and grab its filepath (it might be directly as a property in the listbox or you might have to concatenate the file path with the listbox.Item's file name. So probably just wrapping the original code in a For loop to get all the files in the listbox and changing the file paths to point to what the listbox points to will work.

As far as repopulating the listbox, there's a few ways to do this. You'd might want to keep a list of files you have created in your code and populate the list from there.

Your other option would be to use the file information to determine if the file is one your software has created or not. The best way to do this would probably be to name your files new-test.txt, (or some other prefix) so you can just check if the file begins with new-, and if it does, put it in the listbox.

Let me know if this doesn't make sense.
 
Hi, thanks for your help.

What you're saying makes sense, though I'm trying to work ou how to do it..
Using you code as an example, would something like this work:


VB.NET:
Dim wordsToReplace() As String = {" test ", " Visual ", " Basic "}

       For Each sr As String in listbox1.items

            ' Read the entire file into memory.
            Dim str As String = sr.ReadToEnd()
      Next

            ' Replace all the words with nothing.
            For Each s As String In wordsToReplace
                str = str.Replace(s, " ")
            Next

            ' Write the new file to a different location. Note different filename since C:\test.txt is still open.
            Using sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\test1.txt")
                sw.Write(str)
sw.Close()
End Using
 
You are on the right track, but not there yet. There's a couple of things I would like to point out.

First, please at least try to compile and run any code you post...ReadToEnd is not a method of String, and str is not defined in the second loop, so the code you gave will not compile.

1. Your first loop does not read the file...you should be declaring a System.IO.StreamReader object inside the loop and using it to read the file text into a string.

2. Your first loop (for each sr as string) should enclose the whole method...what you are trying to do is read all the files in your listbox, keep the last one, replace the strings, and save only the last file to a different file.

3. I suggest you take the time to read about local variables. You used a local variable in the following code and later try to access it where it does not "exist". Check out the Microsoft documentation here:
Scope in Visual Basic

VB.NET:
For Each sr As String in listbox1.items
     ' Read the entire file into memory.
     Dim str As String = sr.ReadToEnd()
Next

' STR DOES NOT EXIST HERE.
For Each s As String In wordsToReplace
      str = str.Replace(s, " ")
Next
I am willing to help more, but I would like to see some code that compiles and looks like you have made some effort to getting a solution yourself. The best way to learn (IMHO) is by doing.
 

Latest posts

Back
Top