Reads bytes from file using FSO, Number of bytes can be decided as well as offset bytes.
Not originally my work, but I enhanced it.
Does not check for file existence.
Pass ReadBytes -1 to read full file.
Function ReadFileBytes(FullFileName, OffsetBytes, ReadBytes)
' ReadFileBytes to read certain bytes from any file, using FSO stream
'
' ReadBytes as -1 to read entire file
'
Dim objFSO
Dim objFTemp
Dim objTextStream
Dim lngSize
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFTemp = objFSO.GetFile(FullFileName) ' First, we get the filesize
lngSize = objFTemp.Size
Set objFTemp = nothing
fsoForReading = 1
Set objTextStream = objFSO.OpenTextFile(FullFileName, fsoForReading)
If OffsetBytes > 0 Then
strBuff = objTextStream.Read(OffsetBytes - 1)
End If
If ReadBytes = -1 Then ' Get All!
ReadFileBytes = objTextStream.Read(lngSize) ' ReadAll
Else
ReadFileBytes = objTextStream.Read(ReadBytes)
End if
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
End Function
' ReadFileBytes to read certain bytes from any file, using FSO stream
'
' ReadBytes as -1 to read entire file
'
Dim objFSO
Dim objFTemp
Dim objTextStream
Dim lngSize
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFTemp = objFSO.GetFile(FullFileName) ' First, we get the filesize
lngSize = objFTemp.Size
Set objFTemp = nothing
fsoForReading = 1
Set objTextStream = objFSO.OpenTextFile(FullFileName, fsoForReading)
If OffsetBytes > 0 Then
strBuff = objTextStream.Read(OffsetBytes - 1)
End If
If ReadBytes = -1 Then ' Get All!
ReadFileBytes = objTextStream.Read(lngSize) ' ReadAll
Else
ReadFileBytes = objTextStream.Read(ReadBytes)
End if
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
End Function
FullFileName, OffsetBytes, ReadBytes
Views 189
Downloads 67
CodeID
DB ID
ANmarAmdeen
610
Revisions
v1.0
Monday
January
15
2024