Generate list from string and show on page with VBS

0

Hello, I am trying to read a text file that contains some start characters but it can vary in the amount of elements indicating its cost and in the end the total sum. After generating the list it must be shown on the page in the order in which appear in the file.txt.

File.txt This document has ASCII characters FS, SI and CRLF

EchoFSSIB@ 20 BOX X   1 =    20CRLF
500 BOX X   1 =   500CRLF
        TOTAL =   520 USD

Vbs

    Dim itr
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.GetFile("c:\temp\Archivo.txt")
    If objFile.Size > 0 Then
    Set objReadFile = objFSO.OpenTextFile("c:\temp\Archivo.txt", 1)
    strContents = objReadFile.ReadAll
    itr = strContents
    objReadFile.Close
    End If

    dim scrns
    scrns = Split(itr, Chr(32),-1,1)
    dim s1
    s1 = Replace(itr,scrns(0),"")

    dim scr
    scr = Split(s1,Chr(13),-1,1)


    For count = 0 to Ubound(scr)
        sss = sss + scr(count)& vbNewLine
    Next
    AmountBox.innerhtml = sss
    
asked by felix0000 12.03.2017 в 22:09
source

1 answer

2

Good, probably it is more sensible to read line by line and build the return (Html or the format you want), for this we use the method . ReadLine () of the object TextStream (In this case objReadFile ). Also, in the instance of the TextStream object I advise to use the encoding parameter (ASCII = -2) as follows:

Set objReadFile= objFSO.OpenTextFile("c:\temp\Archivo.txt", 1, False,-2)

For the read loop we use the property AtEndOfStream that is equal to True when it reaches the end of the file, also use a small "trick" with the function Asc () to eventually delete the encoding characters.

Do Until objReadFile.AtEndOfStream 'Or objTextFile.ReadAll
    Line = objReadFile.Readline() 'Return the line and go to the next one
    If Asc(Line) = -15441 or Asc(Line) = 239 Then Line = Mid(Line,4) End IF
    'Use "Line" variable.
Loop

For the use of our current variable Line (Since I do not know the destination format you want to give) it occurs to me to format the items (Assuming they are "[Code] [Name] x [Quantity] = [TotalItems] "or" Total = [Total] ") from the lines with the combined use of the functions InStr () , InStrRev () and Mid () as follows:

If InStr(1, Line, "TOTAL = ") Then      'Si encuentra la "Linea del total"
    Total = Mid(Line, InStr(1, Line, "Total =")+1)
Else                                   'Sino es un articulo
    Codigo = Mid(Line,1,InStr(1, Line, " ")-1)
    Nombre = Mid(Line,InStr(1, Line, " ")+1, InStrRev(Line,"x")-InStr(1, Line, " ")-1)
    Cantidad = CInt(Mid(Line,InStrRev(Line,"x")+1,InStrRev(Line,"=")-InStrRev(Line,"x")-1))
    TotalItem = Mid(Line,InStrRev(Line,"=")+1)
End If

I hope that from here you can continue easier, by the way, You may want to use the functions Cint () (or others data type converters ) or Trim () to leave prolix the return of Mid()

    
answered by 31.01.2018 в 21:44