Home Image-Pro Automation (Macros, Apps, Reports)

read text file line by line

Hi Folks, 

Stored all vars in a simple text file line by line (e.g. ROInum = 3), which works well with standard functions. 

However, when trying readfile functions I simply can't get it running properly. 

Best result was that I can read two entry chars and the very first character in the first line. 

Here one of my tries of such a function. 

Can you help? 

Many thanks

Daniel

Public Function ReadLines(ByVal sFile As String, Optional ByVal nLine As Long = 1) As String

  Dim sLines() As String
  Dim oFSO As Object
  Dim oFile As Object


  On Error GoTo ErrHandler
With Automate.ScriptingCommands.CodeCommand(ReadLines)
If .Run() Then

  ' Verweis auf das FileSystemObject erstellen
  oFSO = CreateObject("Scripting.FileSystemObject")

  ' Existiert die Datei überhaupt?
  If oFSO.fileexists(sFile) Then
    ' Datei öffnen
    oFile = oFSO.opentextfile(sFile)

    ' Alles lesen und in Array zerlegen
    sLines =  Split(oFile.readall, vbCrLf)

    ' Datei schließen
    oFile.Close

    Select Case Sgn(nLine)
       ' (nLine > 0)
      Case 1
        ' n-te Zeile von vorne beginnend
        ReadLines = sLines(nLine - 1)

      ' (nLine < 0)
      Case -1
        ' n-te Zeile von hinten beginnend
        ReadLines = sLines(UBound(sLines) + nLine + 1)
    End Select
  'End If

ErrHandler:
'MsgBox("error")
  ' Objekte zerstören
  oFile = Nothing
  oFSO = Nothing
End If
End With


End Function

Best Answer

Answers

  • edited January 2017
    Hi Daniel,

    It's hard to tell what the problem is without debugging. Also, Scripting.FileSystemObject seems to be an old class from VB6. VB.NET has many classes (in System.IO) that handle file reading, it's fully supported in Premier, for example this:

    https://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.110).aspx 

    Yuri

  • Hi Yuri, 

    Do you have a ready to use solution? I use some older scripts sometimes since I don't want to invent all things new. I guess that the problem might be the 'With Automate.ScriptingCommands.CodeCommand(ReadLines)' block... that has to be used... I'll try to put that into a sub and push the data into an array.. 
    Let's see. 
    Thx

    Daniel
  • 2017-01-30-121624

    Daniel --

    I have extracted and attached the

        LoadSettings_Click SUBROUTINE

    from a PREMIER APP that I wrote. 

    The SUBROUTINE is attached in a TXT FILE named

        2017-01-30-121703.txt

    This SUBROUTINE is designed to read and work with the contents of an INI FILE (ASCII TEXT FILE).

    You will have to edit this file to work with your application but hopefully it will give you guidance about one way to tackle your challenge.

    I hope this information is helpful.

    -- Matt
  • 2017-01-31-105533

    Dan --

    I'm glad we were able to help.

    -- Matt

Sign In or Register to comment.