There is a built in easy way to read files in binary within VBA, however it has a restriction of 2GB (2,147,483,647 bytes - max of Long data type). As technology evolves, this 2GB limit is easily breached. e.g. an ISO image of Operating System install DVD disc. Microsoft does provide a way to overcome this via low level Windows API and here is a backup of it.
Also demonstrate (Read part) for calculating File Hashes without external program like fciv.exe
from Microsoft.
Method Name | Description |
---|---|
IsOpen | Returns a boolean to indicate whether the file is open. |
OpenFile(sFileName As String) | Opens the file specified by the sFileName argument. |
CloseFile | Closes the currently open file. |
ReadBytes(ByteCount As Long) | Reads ByteCount bytes and returns them in a Variant byte array and moves the pointer. |
WriteBytes(DataBytes() As Byte) | Writes the contents of the byte array to the current position in the file and moves the pointer. |
Flush | Forces Windows to flush the write cache. |
SeekAbsolute(HighPos As Long, LowPos As Long) | Moves the file pointer to the designated position from the beginning of the file. Though VBA treats the DWORDS as signed values, the API treats them as unsigned. Make the high-order argument non-zero to exceed 4GB. The low-order DWORD will be negative for values between 2GB and 4GB. |
SeekRelative(Offset As Long) | Moves the file pointer up to +/- 2GB from the current location. You can rewrite this method to allow for offsets greater than 2GB by converting a 64-bit signed offset into two 32-bit values. |
Property | Description |
---|---|
FileHandle | The file handle for the currently open file. This is not compatible with VBA file handles. |
FileName | The name of the currently open file. |
AutoFlush | Sets/indicates whether WriteBytes will automatically call the Flush method. |
Function | Notes |
---|---|
GetFileHash(sFile As String, uBlockSize As Double, sHashType As String) | Simply throw in the full path to be hashed, Blocksize to use (number of bytes), and the type of Hash to use - one of the private constants: HashTypeMD5, HashTypeSHA1, HashTypeSHA256, HashTypeSHA384, HashTypeSHA512. This was designed to be as generic as possible. |
You should un/comment the uFileSize As Double accordingly. I have tested MD5 and SHA1.