Load ASCII code of "alphanumeric chars" in first n bytes of binary file in vbscript (make the code faster)

StackOverflow https://stackoverflow.com/questions/18967710

  •  29-06-2022
  •  | 
  •  

Domanda

I want to load the ascii code of all letters and digits in first n bytes (100000 for example) of a binary file into an array. I wrote this code:

Option Explicit

Dim i, lCharCount, lFileByte, lFileArray(99999)
Dim oFSO, oStream, sInFileName

'Validate input command line
If WScript.Arguments.Count < 1 Then
    MsgBox "No input file has been specified!", vbExclamation, "My Script"
    WScript.Quit
End If

sInFileName = WScript.Arguments(0)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oStream = oFSO.OpenTextFile(sInFileName, 1)
Do While Not oStream.AtEndOfStream
    lFileByte = Asc(oStream.Read(1))
    If (lFileByte > 47 And lFileByte < 58) Or (lFileByte > 64 And lFileByte < 91) Or (lFileByte > 96 And lFileByte < 123) Then
        lFileArray(lCharCount) = lFileByte
        lCharCount = lCharCount + 1
        If lCharCount = 100000 Then Exit Do
    End If
Loop
oStream.Close: Set oStream = Nothing

But I need it to run faster. I'd rather not use ADODB but, I'm open to all suggestions if it can't be sped up using FSO. Any ideas?

È stato utile?

Soluzione

Try something like this:

cnt = 100000

data = oFSO.OpenTextFile(sInFileName).Read(cnt)
ReDim lFileArray(Len(data)-1)

For i = 1 To Len(data)
  lFileArray(i-1) = Asc(Mid(data, i, 1))
Next

Altri suggerimenti

Try calling ReadAll on your file instead char by char. This will read the entire file and return it as a string. Then use the same loop, but this time on the returned string, using string scan methods.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top