stringinfile.txt

来自「检测文件中是否包含指定字符串」· 文本 代码 · 共 53 行

TXT
53
字号
'****************************************************************
' Name: Does a file contain a string?
' Description: Determines whether a file contains a string
' By:Daniel Wiman
'
' Inputs:None
' Returns:None
' Assumes:None
' Side Effects:None
' Requires: Nothing
'
'Code provided by Robert's Visual Basic Homepage 'as is', without
'     warranties as to performance, fitness, merchantability,
'     and any other warranty (whether expressed or implied).
'****************************************************************

Function FileContains (FileName As String, SearchText As String) As Long
Dim FileNumber As Integer
Dim FileLength As Long
Dim Chunk As String
Dim ChunkStart As Long
Dim FoundAt as LongConst MaxChunk = 20000
On Error GoTo FileContainsError
FileNumber = FreeFile
Open FileName For Binary Access Read Shared As FileNumber
FileLength = LOF(FileNumber)
ChunkStart = 0
Do Until ChunkStart = FileLength
    If FileLength - ChunkStart > MaxChunk Then
        Chunk = Input$(MaxChunk, FileNumber)
        ChunkStart = ChunkStart + MaxChunk - Len(SearchText)    
     Else
        Chunk = Input$(FileLength - ChunkStart, FileNumber)
        ChunkStart = FileLength    
    End If
        FoundAt = InStr(Chunk, SearchText)    
	If FoundAt > 0 Then
        FileContains = FoundAt        
	Exit Do    
End If
Loop
Close FileNumber
Exit Function

FileContainsError:    

Select Case Err        
	Case Else
            MsgBox Error$ & " on file " & FileName    
	    End Select
    	Exit Function
End Function

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?