Here is a very simple example routine which reads a file from disk, in one big lump. This uses the built-in VB statements only. Later we will cover the FileSystemObjec t, which provides greater functionality at the expense of slightly greater code complexity.
This self-contained routine can be pasted into a code module and called from anywhere, including the immediate window. It will expect you to pass the name of a file (including the path if it isn't in the current directory) and will copy the contents of the file to the immediate window. Note it will also avoid, as far as possible, interfering with any other processing which may be going on at the time.
The extra parameter (compared to the prior sample in this series) allows you to request that the file contents be dumped as one big string (appropriate for a text file) or byte by byte (appropriate for most other file types).
[CODE=vb]Public Sub DumpFile_V02(By Val FileName As String, ByVal TreatAsText As Boolean)
Dim FileNo As Long
Dim FileSize As Long
Dim Buffer As String
Dim CharNo As Long, Char As String * 1
FileNo = FreeFile ' Get next available file number.
Open FileName For Binary Access Read Shared As #FileNo
FileSize = LOF(FileNo) ' Determine how large the file is (in bytes).
Buffer = Space$(FileSize ) ' Set our buffer (string) to that length.
' The length of the string (Buffer) determines how many bytes are read...
Get #FileNo, , Buffer ' Grab a chunk of data from the file.
Close #FileNo
' Display the results, either as one big chunk or byte-by-byte.
If TreatAsText Then
Debug.Print Buffer
Else
For CharNo = 1 To FileSize
Char = Mid(Buffer, CharNo, 1)
Debug.Print Format(CharNo, "#,###"); " = Decimal(" _
; Format(Asc(Char ), "000"); ") Hexadecimal(" _
; Hex$(Asc(Char)) ; ") ["; Char; "]"
DoEvents
Next
Beep ' Just let the user know we're finished.
End If
End Sub[/CODE]
This self-contained routine can be pasted into a code module and called from anywhere, including the immediate window. It will expect you to pass the name of a file (including the path if it isn't in the current directory) and will copy the contents of the file to the immediate window. Note it will also avoid, as far as possible, interfering with any other processing which may be going on at the time.
The extra parameter (compared to the prior sample in this series) allows you to request that the file contents be dumped as one big string (appropriate for a text file) or byte by byte (appropriate for most other file types).
[CODE=vb]Public Sub DumpFile_V02(By Val FileName As String, ByVal TreatAsText As Boolean)
Dim FileNo As Long
Dim FileSize As Long
Dim Buffer As String
Dim CharNo As Long, Char As String * 1
FileNo = FreeFile ' Get next available file number.
Open FileName For Binary Access Read Shared As #FileNo
FileSize = LOF(FileNo) ' Determine how large the file is (in bytes).
Buffer = Space$(FileSize ) ' Set our buffer (string) to that length.
' The length of the string (Buffer) determines how many bytes are read...
Get #FileNo, , Buffer ' Grab a chunk of data from the file.
Close #FileNo
' Display the results, either as one big chunk or byte-by-byte.
If TreatAsText Then
Debug.Print Buffer
Else
For CharNo = 1 To FileSize
Char = Mid(Buffer, CharNo, 1)
Debug.Print Format(CharNo, "#,###"); " = Decimal(" _
; Format(Asc(Char ), "000"); ") Hexadecimal(" _
; Hex$(Asc(Char)) ; ") ["; Char; "]"
DoEvents
Next
Beep ' Just let the user know we're finished.
End If
End Sub[/CODE]