'注释 file.path=app.path
'对当前某个文件的行数的读取计算
Dim hT1 As Integer, hCount As Integer, hFreeNumber As Integer
Dim tmp As String
tmp = ""
hCount = 0
For hT1 = 0 To File.ListCount - 1
If File.Selected(hT1) = True Then
hFreeNumber = FreeFile
If Len(dir.Path) > 3 Then
Open dir.Path + "\" + File.List(hT1) For Input As #hFreeNumber
Else
Open dir.Path + File.List(hT1) For Input As #hFreeNumber
End If
While Not EOF(hFreeNumber)
Line Input #hFreeNumber, tmp
hCount = hCount + 1
If tmp = "" Then hCount = hCount - 1
Wend
Close #hFreeNumber
End If
Next
MsgBox "计算完毕!"
labTot.Caption = "档案总共行数" + Chr$(13) + Chr$(10) + Str$(hCount)
For tIndex = 0 To tIndexOver
tOutLng = tOutLng + ((tBytes(tIndex) = 13) And 1)
'以上相当于语句:If tBytes(tIndex) = 13 Then tOutLng = tOutLng + 1
Next
TextLineCountGet = tOutLng
End Function
Function TextLineCountGet_ForView(ByVal pText As String, Optional pViewTextBox As TextBox) As Long
'进度指示版。可完全按照TextLineCountGet来用,当你指定pViewTextBox后,会在该TextBox显示进度。
Dim tOutLng As Long
Dim tBytes() As Byte
Dim tIndex As Long
Dim tIndexOver As Long
Dim tEventStep As Long
For tIndex = 0 To tIndexOver
tOutLng = tOutLng + ((tBytes(tIndex) = 13) And 1)
'以上相当于语句:If tBytes(tIndex) = 13 Then tOutLng = tOutLng + 1
If Not CBool(tIndex Mod tEventStep) Then
If Not pViewTextBox Is Nothing Then
pViewTextBox.Text = tIndex * 100 \ tIndexOver
DoEvents
End If
End If
Next
End If
TextLineCountGet_ForView = tOutLng
End Function
Function TextGetByFile(ByVal pFileName As String) As String
'从文件获得多行文本。
Dim tOutText As String
Dim tFileNumber As Integer
Dim tBytes() As Byte
tFileNumber = FreeFile
Open pFileName For Binary As #tFileNumber
If CBool(LOF(tFileNumber)) Then
ReDim tBytes(LOF(tFileNumber) - 1)
Get tFileNumber, 1, tBytes()
tOutText = StrConv(tBytes, vbUnicode)
Else
MsgBox "警告:文件" & pFileName & "是空的!", vbOKOnly, "文件无内容"
End If
Close #tFileNumber
TextGetByFile = tOutText
End Function
还可以这样:
Private Sub Command2_Click()
Dim tBytes() As Byte
Dim tFileSize As Long
Dim tIndex As Long
Dim tIndexOver As Long
Dim tLineCount As Long
Open "SY.txt" For Binary As #1
tFileSize = LOF(1) - 1
ReDim tBytes(tFileSize)
tIndexOver = tFileSize
Get #1, 1, tBytes()
Close #1
For tIndex = 0 To tIndexOver
tLineCount = tLineCount + ((tBytes(tIndex) = 13) And 1)
Next
Text1.Text = tLineCount
End Sub
稍后给你函数。
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.CursorLocation = adUseClient
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\;Extended Properties=Text;Persist Security Info=False;Mode=Share Deny None"
rs.Open "select 1 from [test#txt]", cn
Debug.Print "文本c:\test.txt共有行数为:" & rs.RecordCount
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
dim mfile as string
mfile="c:\test.txt"
dim buff() as byte
dim i as long
i=filelen(mfile)
redim buff(i-1)
open mfile for binary as #1
get #1,,buff
close #1
dim s as string
s=strconv(buff,vbunicode)
erase buff
dim arr() as string
arr=split(s,vbcrlf)
msgbox "文件共有行数:"+cstr(ubound(arr)+1)