Sunday, June 17, 2012

Membaca File Binary atau Text Dengan Cepat

'Kode ini dibuat oleh plenderj salah satu member VBForums
'http://www.vbforums.com/showthread.php?s=&threadid=132171

' Return a specific line number from a file (note: first line = line number 0)
Private Function getLine(ByVal strFile As String, ByVal lineNumber As Long) As String
Dim strBuff As String
Open strFile For Binary As #1
strBuff = Space(LOF(1))
Get #1, , strBuff
getLine = Split(strBuff, vbCrLf)(lineNumber)
Close #1
End Function

' Return a specific line number from a file (note: first line = line number 0) - a neater version.
Private Function getLine(ByVal strFile As String, ByVal lineNumber As Long) As String
getLine = Split(returnContents(strFile), vbCrLf)(lineNumber)
End Function

' Delete a specific line from a file (note: first line = line number 0)
Private Sub deleteLine(ByVal strFile As String, ByVal lineNumber As Long)
Dim strArrBuff() As String, i As Long, strFileContent As String
strArrBuff() = Split(returnContents(strFile), vbCrLf)
strArrBuff(lineNumber) = vbNullString
Open strFile For Output As #1
Print #1, Join(strArrBuff, vbCrLf);
Close #1
End Sub

' Return the contents of a file
Private Function returnContents(ByVal strFile As String) As String
Dim strBuff As String
Open strFile For Binary As #1
strBuff = Space(LOF(1))
Get #1, , strBuff
returnContents = strBuff
Close #1
End Function