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).
Expand|Select|Wrap|Line Numbers
- Public Sub DumpFile_V02(ByVal 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