Tuesday, March 23, 2010

Search for text files in your drives and display them

Recently I received an email from a reader who ask me a VB question as follows:

' - I have a folder and file name with *.txt in C: -> C:\Test
 - I want to create a vb program to search all *.txt extension file
 - If found then just desplay it to textbox.

How can we write this code? "


Well, I have one sample program that might fit this need. This program let you choose your drives and folders and displays all the text files from the chosen folder in a list box. You need to place a combo box, a DriveListBox, a DirListbox and a Listbox into your form. The code is as follows:





Private Sub Form_Load()
Left = (Screen.Width - Width) \ 2
Top = (Screen.Height - Height) \ 2
Combo1.Text = "All Text Files"
Combo1.AddItem "All Text Files"
Combo1.AddItem "All Files"

End Sub

Private Sub Combo1_Change()
If ListIndex = 0 Then
File1.Pattern = ("*.txt")
Else
Fiel1.Pattern = ("*.*")
End If

End Sub


Private Sub Dir1_Change()
File1.Path = Dir1.Path
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.txt")
Else

File1.Pattern = ("*.*")
End If
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub



Private Sub File1_Click()
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.txt")

Else
File1.Pattern = ("*.*")
End If

If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
Text1.Text = filenam

End Sub

No comments: