PDA

View Full Version : Đoạn code tìm một chuỗi bên trong một file



Cuni
10-09-2009, 10:02 AM
Bạn cần dò trong một file để tìm ra chuỗi nào đó.
Duyệt qua file từng ký tự một bằng phương thức FileStream.ReadByte, và cố gom thành chuỗi cần tìm.
Dễ dàng hiện thực phép tìm kiếm toàn văn (full-text searching), mặc dù có thể tốn nhiều thời gian. Những gì bạn cần làm là duyệt qua file, đọc mỗi byte và chuyển nó thành một ký tự. Nếu đọc một ký tự trùng khớp với chuỗi cần tìm, bạn có thể kiểm tra ký tự kế tiếp có trùng khớp hay không, và tiếp tục.
Lớp FileTextSearcher dưới đây đóng gói các chức năng cần thiết khi thực hiện một phép tìm kiếm toàn văn với bất kỳ kiểu file nào.

Public Class FileTextSearcher

Private _Matches As New ArrayList
Private _FileFilter As String
Private _SearchText As String
Private _CaseSensitive As Boolean = True

Public ReadOnly Property Matches() As ArrayList
Get
Return _Matches
End Get
End Property

Public Property FileFilter() As String
Get
Return _FileFilter
End Get
Set(ByVal Value As String)
_FileFilter = Value
End Set
End Property

Public Property SearchText() As String
Get
Return _SearchText
End Get
Set(ByVal Value As String)
_SearchText = Value
End Set
End Property
Public Property CaseSensitive() As Boolean
Get
Return _CaseSensitive
End Get
Set(ByVal Value As Boolean)
_CaseSensitive = Value
End Set
End Property

Public Sub New(ByVal fileFilter As String, _
ByVal searchText As String)
Me.FileFilter = fileFilter
Me.SearchText = searchText
End Sub
Public Sub Search(ByVal startingPath As String)
Matches.Clear()
SearchDirectory(New DirectoryInfo(startingPath))
End Sub
Private Sub SearchDirectory(ByVal dir As DirectoryInfo)
' Lấy các file trong thư mục này.
Dim FileItem As FileInfo
For Each FileItem In dir.GetFiles(FileFilter)
' Kiểm tra file này có trùng khớp hay không.
If TestFileForMatch(FileItem) Then
Matches.Add(FileItem)
End If
Next
' Bạn có thể thêm logic đệ quy ở đây bằng cách gọi
' SearchDirectory trên tất cả các thư mục con (xem mục 5.7).
End Sub
Private Function TestFileForMatch(ByVal file As FileInfo) As Boolean
' Mở file.
Dim fs As FileStream = file.OpenRead()
Dim Match As Boolean = False
Dim MatchCount, MatchPosition As Integer
Dim Character, MatchCharacter As String
' Đọc qua toàn bộ file.
Do Until fs.Position = fs.Length
' Lấy một ký tự từ file.
Character = Convert.ToChar(fs.ReadByte())
' Lấy ký tự kế tiếp từ chuỗi cần tìm để đem so trùng.
MatchCharacter = SearchText.Substring(MatchPosition, 1)
If String.Compare(Character, MatchCharacter, _
Not Me.CaseSensitive) = 0 Then
' Chúng trùng khớp. Bây giờ thử so trùng ký tự kế tiếp.
MatchPosition += 1
Else
' Chúng không trùng khớp. Làm lại từ đầu.
MatchPosition = 0
End If
' Kiểm tra toàn bộ chuỗi đã được so trùng hay chưa.
If MatchPosition = SearchText.Length - 1 Then
Return True
End If
Loop
fs.Close()
Return False
End Function
End Class
Dưới đây là cách sử dụng lớp này để tìm trong các file mã lệnh Visual Basic một biến có tên là MyVariable:

Dim Searcher As New FileTextSearcher("*.vb", "MyVariable")
Searcher.Search("C:\Temp")

' Hiển thị kết quả.
Dim File As FileInfo
For Each File In Searcher.Matches
Console.WriteLine(File.FullName)
Next
Trích từ “Các giải pháp lập trình VISUAL BASIC .NET” (http://www.dvpub.com.vn/dv/details.aspx?itemid=244)

hackerkinhcan_lha
14-09-2009, 11:40 AM
Hi ý tưởng thế này đc hông :D
gán biến a cho line input

dùng hàm instring checkl nếu ok -->true

Dùng do loop đến hết tệp ;)