Read a binary file into an array - file-io

What's the fastest way (using VB6) to read an entire, large, binary file into an array?

Here's one way, although you are limited to files around 2 GB in size.
Dim fileNum As Integer
Dim bytes() As Byte
fileNum = FreeFile
Open "C:\test.bin" For Binary As fileNum
ReDim bytes(LOF(fileNum) - 1)
Get fileNum, , bytes
Close fileNum

You can compare these two
Private Function ReadFile1(sFile As String) As Byte()
Dim nFile As Integer
nFile = FreeFile
Open sFile For Input Access Read As #nFile
If LOF(nFile) > 0 Then
ReadFile1 = InputB(LOF(nFile), nFile)
End If
Close #nFile
End Function
Private Function ReadFile2(sFile As String) As Byte()
Dim nFile As Integer
nFile = FreeFile
Open sFile For Binary Access Read As #nFile
If LOF(nFile) > 0 Then
ReDim ReadFile2(0 To LOF(nFile) - 1)
Get nFile, , ReadFile2
End If
Close #nFile
End Function
I prefer the second one but it has this nasty side effect. If sFile does not exists For Binary mode creates an empty file no matter that Access Read is used.

Related

GetFiles is incredibly slow vb.NET

I need to process a large amount of files and scour thru a group of directories and subdirectories and get the file from the found directory. This is easy using a simple directory but when scouring a thousand directories, the program slows down to a crawl. I've heard of using enumeratefiles but I'm not how to approach this. thank you.
How should I approach this?
dim Filename_to_lookfor as string
dim filePaths As String()
for i = 0 to 1000
Filename_to_lookfor = array(i)
filePaths = Directory.GetFiles(sTargetPath, sFilename_to_lookfor.ToUpper,
IO.SearchOption.AllDirectories)
if filepath.length > 0 then
'copy file using Computer.FileSystem.CopyFile()
end if
next
Thank you.
Another coding would be using Linq:
Sub SearchFiles(pathAndfileNames_to_lookfor() As String, sTargetPath As String)
' Call just once Directory.GetFiles():
Dim FilePaths() As String = Directory.GetFiles(sTargetPath, "*.*", IO.SearchOption.AllDirectories)
Dim lookup1 = FilePaths.ToLookup(Function(x) x.ToLower)
Dim lookup2 = pathAndfileNames_to_lookfor.ToLookup(Function(x) x.ToLower)
Dim filesInBoth = lookup1.SelectMany(Function(x) x.Take(lookup2(x.Key).Count).ToArray)
For Each file In filesInBoth
'copy file using Computer.FileSystem.CopyFile()
Console.WriteLine(file)
Next
End Sub
Calling the procedure would be as follows:
Dim file1 As String = "C:\...\file1..."
Dim file2 As String = "C:\...\file2..."
Dim pathAndfileNames_to_lookfor() As String = New String() {file1, file2}
Dim sTargetPath = "..."
SearchFiles(pathAndfileNames_to_lookfor, sTargetPath)

vba base64 image png decode

I have big problem in scraping webpage
I needed scraping img src adrress
but
result is
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGP6zwAAAgcBApocMXEAAAAASUVORK5CYII="
At first I didn't this but after I Knew that
but I can't decode
I try to search about base64 png image but I can't try to code at all.
I need your help
Give this a try. I wrote two functions DecodeBase64 and WriteByteArrayToFile. DecodeBase64 takes in a Base64 encoded string and returns the Byte() from this. WriteByteArrayToFile takes in a Byte() and a FilePath as string, and will write this Byte() to a file.
Update this section "YOURPATHGOESHERE\Picture.png" in the Example Sub to a valid file path on your computer, see if this code does what you are after. I'm getting a very small picture of what appears to be a square when running the code below.
Code
Option Explicit
'Decode Base64 string into a byte array
Public Function DecodeBase64(ByVal Base64String As String) As Byte()
With CreateObject("MSXML2.DOMDocument").createElement("b64")
.DataType = "bin.base64"
.Text = Base64String
DecodeBase64 = .nodeTypedValue
End With
End Function
'Take a byte array and write to a file
Public Sub WriteByteArrayToFile(FileData() As Byte, ByVal FilePath As String)
Dim FileNumber As Long: FileNumber = FreeFile
Open FilePath For Binary Access Write As #FileNumber
Put #FileNumber, 1, FileData
Close #FileNumber
End Sub
'Run from here
Sub example()
WriteByteArrayToFile DecodeBase64("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGP6zwAAAgcBApocMXEAAAAASUVORK5CYII="), "YOURPATHGOESHERE\Picture.png"
End Sub

VBA calculate MD5 hash on file contents

I need a VBA routine to calculate the MD5 hash of a file's contents. I located some examples (e.g., here) but I found that they crashed when the filename contained certain Unicode characters, so I am trying to tweak the code to avoid that.
This code does not result in an error, but it also doesn't return the correct MD5 hash. What's wrong?
Public Function FileToMD5Hex(sFileName As String) As String
Dim enc
Dim bytes
Dim outstr As String
Dim pos As Integer
Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
'Convert the string to a byte array and hash it
bytes = GetFileBytes(sFileName)
bytes = enc.ComputeHash_2((bytes))
'Convert the byte array to a hex string
For pos = 1 To LenB(bytes)
outstr = outstr & LCase(Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2))
Next
FileToMD5Hex = outstr
Set enc = Nothing
End Function
Private Function GetFileBytes(path As String) As Byte()
Dim fso As Object
Set fso = CreateObject("scripting.FileSystemObject")
Dim fil As Object
Set fil = fso.GetFile(path)
' Dim fpga As Variant
GetFileBytes = fil.OpenAsTextStream().Read(fil.Size)
Set fil = Nothing
Set fso = Nothing
End Function
There are some chars sequences that Scripting.FileSystemObject can't process properly as TextStream.
Use ADODB.Stream ActiveX to retrieve array of bytes from file. It works perfectly with both text and binary types of data, also it allows to change charset of the string (FSO only works with ASCII and Unicode, and only with files).
Function GetFileBytes(strPath As String) As Byte()
With CreateObject("ADODB.Stream")
.Type = 1 ' adTypeBinary
.Open
.LoadFromFile (strPath)
GetFileBytes = .Read()
End With
End Function
Another one ActiveX processing binary data is SAPI.spFileStream. One of the most significant advantages - it allows to load only the part of the file to the memory (in some cases when comparing large files it can help drastically increase performance, checking md5 by chunks).
Function GetFileBytes(strPath As String) As Byte()
Dim arrContent As Variant
With CreateObject("SAPI.spFileStream")
.Open strPath, 0
.Read arrContent, CreateObject("Scripting.FileSystemObject").GetFile(strPath).Size
.Close
End With
GetFileBytes = arrContent
End Function

VB.NET: GetFiles() in Specific Directory

Currently the line of code looks like this:
Dim files() As String = System.IO.Directory.GetFiles(path, filehead & ".*.*.fsi")
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String
Dim file As String = ""
For Each file In files
args = Split(file, ".")
If args.Length = 4 Then
seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
End If
The problem is, sometimes, in my case, the path looks something like:
C:\Users\c.brummett\Downloads
and the split causes a split in the username. How can I avoid this problem but still split by periods? I'm sorry I don't how to make this more relatable.
My idea was to use a DirectoryInfo and do something like:
Dim di As DirectoryInfo
di = New DirectoryInfo(path)
Dim files() As String = di.GetFiles(filehead & ".*.*.fsi")
Edit: The problem with this second bit of code, is that it returns the error
Value of type '1-dimensional array of System.IO.FileInfo' cannot be converted to '1-> dimensional array of String' because 'System.IO.FileInfo' is not derived from 'String'.`
You can forget about getting an array of file names (you don't need that anyway) and iterate on the array of FileInfo:
Dim files() As FileInfo = New DirectoryInfo(path).GetFiles(filehead & ".*.*.fsi")
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String
For Each file As FileInfo In files
args = Split(file.Name, ".")
If args.Length = 4 Then
seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
End If
Note AllDirectories and change in the line doing the splitting. I didn't look at your array structure stuff.
Dim files() As String = System.IO.Directory.GetFiles("C:\temp", "*.doc", IO.SearchOption.AllDirectories)
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String
Dim file As String = ""
For Each file In files
args = file.Substring(file.LastIndexOf("\") + 1).Split(".")
If args.Length = 4 Then
seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
End If
Next file

how to get value from spilt function in to string array

i want to use split function for splitting string into two parts.The question is how can i save the first part and second part into two different string array for further use?
The text i'm going to process is Name,filepath
Dim clientArr() as string
Dim FileArr() as string
FileOpen(1, strpath & "Location.txt", OpenMode.Input)
Do Until EOF(1)
Templine = LineInput(1)
Dim DataArr() As String = Templine.Split(",")
ClientArr = DataArr(0)
FileArr = DataArr(1)
loop
The error says that string cannot be convented into 1 D array
Thank you
It is the 21st Century and along with flying cars and robot dogs we have a easy way to keep 2 large sets of related data together in a single container, but able to be referenced individually:
Class myFileParts
public Property FileName As String
public Property FilePath As String
Public Sub New(fName as string, fPath as String)
FileName = fName
FilePath = fPath
End Sub
End Class
' a container to hold lots of these
Friend myFiles As New List(Of myFileParts)
' filling it up:
Dim TempLine As String
Dim myF As myFileParts
Do Until EOF(1)
Templine = LineInput(1)
Dim DataArr() As String = Templine.Split(",")
' create a file item
f = New myFileParts(DataArr(0),DataArr(1))
' add to container
myFiles.Add(f)
loop
to use one: (N is the index of the desired file info):
Dim thisFile As string = MyFiles(N).FileName
Dim thisPath As string = MyFiles(N).FilePath
to print them all:
For each f As myFileParts in myFiles
Console.WriteLine(String.Format("Path = {0}; Name = {1}",
f.FilePath, f.FileName)
Next f
The problem is that you declare ClientArr and FileArr as string arrays, which is what you want, but then you try to assign them a string value. DataArr(0) is a string value. You need to put that vcalue into one of the array elements in ClientArr. E.g. CleintArr(5) = DataArr(0).
If you knew the exact number of line in the file before the loop then you could declare the array size for CleintArr and FileArr. Then you would use an index that gets increments in the loop to set the proper array element.
But here is a better approach that will work without knowing the size of the file beforehand. This uses string collections to accumulate the clients and files, and then it converts them to an array once the file has been read.
Dim clientArr() As String
Dim FileArr() As String
Dim ClientList As New List(Of String)
Dim FileList As New List(Of String)
FileOpen(1, strpath & "Location.txt", OpenMode.Input)
Do Until EOF(1)
Templine = LineInput(1)
Dim DataArr() As String = Templine.Split(",")
ClientList.Add(DataArr(0))
FileList.Add(DataArr(1))
Loop
clientArr = ClientLIst.ToArray()
FileArr = FileList.ToArray()
Do you know how many lines are in the file before you start? If not, you could do something like this using the List class:
Dim clientPart As List(Of String) = New List(Of String)()
Dim filePart As List(Of String) = New List(Of String)()
FileOpen(1, strpath & "Location.txt", OpenMode.Input)
Do Until EOF(1)
Templine = LineInput(1)
Dim DataArr() As String = Templine.Split(",")
clientPart.Add(DataArr(0))
filePart.Add(DataArr(1))
Loop
Dim clientPartStringArray As String() = clientPart.ToArray() 'If you really want an array
If you do, then you need to assign each split to an element of your clientArr and fileArr like:
Dim numLinesInFile As Integer 'If you know this value
Dim curIndex As Integer = 0
Dim clientArray(numLinesInFile) As String
Dim fileArray(numLinesInFile) As String
FileOpen(1, strpath & "Location.txt", OpenMode.Input)
Do Until EOF(1)
Templine = LineInput(1)
Dim DataArr() As String = Templine.Split(",")
clientArray(curIndex) = DataArr(0)
fileArray(curIndex) = DataArr(1)
curIndex = curIndex + 1
Loop
Note the differences in your code in terms of:
We declare the arrays clientArray and fileArray with a specific size (you'll need to know this in advance - if you don't/can't know this consider using a collection like a List as previously alluded to)
On each iteration we assign the strings you get from the Split into a specific index of the array denoted by curIndex
We have to increment curIndex by 1 each time so that we write to empty slots in clientArray and fileArray...otherwise you'd just write to the same index (0) each time