954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

VB : Delete words smaller then 5 letter from a file

Hello !
I just downloaded a Dictionary Words list .txt( containing all dictionary words ), every word is on a line.
What i need is only words > 5 letters ( length > 5 ). S o i though about making a Visual basic software that delete words < 5 letters from the file.

This is how i read a file from VB

Dim FILE_NAME As String = "C:\Users\Owner\Documents\test.txt"
Dim TextLine As String

If System.IO.File.Exists(FILE_NAME) = True Then

Dim objReader As New System.IO.StreamReader(FILE_NAME)

Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine() & vbNewLine
Loop

Textbox1.Text = TextLine

Else

MsgBox("File Does Not Exist")

End If


Any WAy could you help me with the code ? How to delete the word smaller then 5 letters ?

I am using Visual basic 2008
And Visual Basic language
The list is in .txt format

Thanks :)

a1a4a
Newbie Poster
18 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 

I think something like that is more suited to vbScript than VB. The following script takes 2 arguments, infile and outfile. Infile contains the unfiltered dictionary (one word per line). Outfile is the file to contain the filtered words (will be created if it does not exist). Here is the code

Const ForReading   = 1
Const ForWriting   = 2
Const ForAppending = 8

set fso = CreateObject("Scripting.FileSystemObject")
set arg = Wscript.Arguments

if arg.Count <> 2 Then
   wscript.Echo "delete5 infile outfile"
   wscript.Quit
end if

if Not fso.FileExists(arg(0)) then
   wscript.Echo "file",arg(0),"not found"
   wscript.Quit
end if

set tsoin  = fso.OpenTextFile(arg(0),ForReading)
set tsoout = fso.OpenTextFile(arg(1),ForWriting,True)

do until tsoin.AtEndOfStream

   line = Trim(tsoin.ReadLine)
   
   if Len(line) > 5 Then
      tsoout.WriteLine(line)
   end if
   
loop

tsoin.Close()
tsoout.Close()
Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

Also you can modify slightly your code to not include the short lines like:

Dim FILE_NAME As String = "C:\Users\Owner\Documents\test.txt"
Dim TextLine As String

If System.IO.File.Exists(FILE_NAME) = True Then

    Dim objReader As New System.IO.StreamReader(FILE_NAME)

    Do While objReader.Peek() <> -1
        '
        ' Red a line
        '
        Dim CurLine as String = objReader.ReadLine().Trim() 
        '
        '  Add it only if length > 5 
        '
        if Curline.Length > 5 then TextLine = TextLine & Curline & vbNewLine
    Loop

    Textbox1.Text = TextLine

Else

    MsgBox("File Does Not Exist")

End If


That is only another way.
Hope this helps

lolafuertes
Master Poster
793 posts since Oct 2008
Reputation Points: 120
Solved Threads: 166
 

Also you can modify slightly your code to not include the short lines like:

Dim FILE_NAME As String = "C:\Users\Owner\Documents\test.txt"
Dim TextLine As String

If System.IO.File.Exists(FILE_NAME) = True Then

    Dim objReader As New System.IO.StreamReader(FILE_NAME)

    Do While objReader.Peek() <> -1
        '
        ' Red a line
        '
        Dim CurLine as String = objReader.ReadLine().Trim() 
        '
        '  Add it only if length > 5 
        '
        if Curline.Length > 5 then TextLine = TextLine & Curline & vbNewLine
    Loop

    Textbox1.Text = TextLine

Else

    MsgBox("File Does Not Exist")

End If

That is only another way. Hope this helps


Nice code. Just out of curiosity, in this case, does Visual Basic starts counting the lenght of the line at 0 or it starts at 1. Just curious.

BDove
Newbie Poster
15 posts since Jan 2012
Reputation Points: 10
Solved Threads: 2
 

Length is the actual number of chars. eg Len("abc") = 3

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: