Reading text file do until...

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Reading text file do until...

Post by sal21 »

I need to intercept the string INIT and start from here and loop teh rest of txt until the code interept END...

is correct this code?

Code: Select all

Option Explicit
Sub TEST()

    Dim MY_LINE As String, CONTA As Long

    CONTA = Empty
    
    'FOR SCURITY
    Close #1
    'FOR SCURITY

    Open "C:\TEMP\TABULATI_132.txt" For Input As #1

        While Not Mid$(MY_LINE, 20, 4) = "INIT"
            Line Input #1, MY_LINE
        Wend
        
        While Not Mid$(MY_LINE, 1, 3) = "END"
            Line Input #1, MY_LINE
            CONTA = CONTA + 1
        Wend
        
        Close #1
        
        Exit Sub

End Sub

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reading text file do until...

Post by HansV »

1. You don't have to use

CONTA = Empty

at the beginning. The declaration Dim CONTA ... initiates CONTA to 0.

2. You don't have to use

Exit Sub

at the end. The code will automatically end at End Sub.

3. At the end, CONTA will include the line that starts with END. If you do not want to include that line in the count, insert a line

CONTA = CONTA - 1

after the second loop.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: Reading text file do until...

Post by sal21 »

HansV wrote:1. You don't have to use

CONTA = Empty

at the beginning. The declaration Dim CONTA ... initiates CONTA to 0.

2. You don't have to use

Exit Sub

at the end. The code will automatically end at End Sub.

3. At the end, CONTA will include the line that starts with END. If you do not want to include that line in the count, insert a line

CONTA = CONTA - 1

after the second loop.
good! EXPLAIN!!

But really understand my condition and wath is my Goal?

New way? or...

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reading text file do until...

Post by HansV »

It depends on how many lines the file contains.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: Reading text file do until...

Post by sal21 »

HansV wrote:It depends on how many lines the file contains.
in medium approx 2.300.000 lines :groan: :sad: :groan: :sad:

File generate from a CICS application in a server dir at every days!!!!!!!

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reading text file do until...

Post by HansV »

In that case, you can't read the file into Excel, so you'll have to use something like the code you wrote, but it might be slow.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: Reading text file do until...

Post by sal21 »

HansV wrote:In that case, you can't read the file into Excel, so you'll have to use something like the code you wrote, but it might be slow.

resolved... this!

Now based the piece of code how to store all line processed in array and loop it?

Code: Select all

While Not Mid$(MY_LINE, 1, 3) = "END"
            Line Input #1, MY_LINE
'here fill array with  MY_LINE
            CONTA = CONTA + 1
        Wend
'and here loop it

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reading text file do until...

Post by HansV »

Why do you want to store the values in an array? Wouldn't it be more efficient to process the values in the While ... Wend loop?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: Reading text file do until...

Post by sal21 »

HansV wrote:Why do you want to store the values in an array? Wouldn't it be more efficient to process the values in the While ... Wend loop?
ok if this is a good idea.. ok...

But really my goal is to fill a userform with a textbox named tbox1 with all line from the loop:

example:

1) i double click in a listview on userform1 show new userform2 with tbox1
2) fill the tbox1 with the loop

Code: Select all

While Not Mid$(MY_LINE, 1, 3) = "END"
            Line Input #1, MY_LINE
me.txtbox1  MY_LINE here a carriage return
me.txtbox1  MY_LINE here a carriage return
...
ecc...
'here fill array with  MY_LINE
            CONTA = CONTA + 1
        Wend
'and here loop it
3)close it (with botton"Chiudi")

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reading text file do until...

Post by HansV »

You could use something like this:

Code: Select all

    Dim strText As String
    ...
    strText = ""
    Do While Not Mid$(MY_LINE, 1, 3) = "END"
        Line Input #1, MY_LINE
        strText = strText & vbCrLf & MY_LINE
        CONTA = CONTA + 1
    Loop
    If strText <> "" Then
        Me.TextBox1 = Mid(strText, 3)
    End If
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: Reading text file do until...

Post by sal21 »

HansV wrote:You could use something like this:

Code: Select all

    Dim strText As String
    ...
    strText = ""
    Do While Not Mid$(MY_LINE, 1, 3) = "END"
        Line Input #1, MY_LINE
        strText = strText & vbCrLf & MY_LINE
        CONTA = CONTA + 1
    Loop
    If strText <> "" Then
        Me.TextBox1 = Mid(strText, 3)
    End If
My last code:

Code: Select all

Sub TEST_SHOW_OP()

  Dim MIA_STRINGA As String, CONTA As Long, MIA_RIGA As String

  CONTA = Empty
  MIA_RIGA = ""
  
  Close #1

  Open "C:\TEMP\TABULATI_132.txt" For Input As #1

  Do Until EOF(1)
    While Not Mid$(MIA_STRINGA, 20, 17) = OPERAZ1
      Line Input #1, MIA_STRINGA
    Wend
    MIA_RIGA = MIA_RIGA & MIA_STRINGA & Chr(13)
    Me.SCROLL_OP.Text = MIA_RIGA
    CONTA = Empty
    While Not Mid$(MIA_STRINGA, 1, 30) = "---------- OPERAZIONE ESEGUITA"
      Line Input #1, MIA_STRINGA
      MIA_RIGA = MIA_RIGA & Chr(13) & MIA_STRINGA
      I = I + 1
      CONTA = CONTA + 1
    Wend
    Close #1
      Me.SCROLL_OP.Text = MIA_RIGA
      Exit Sub
  Loop
End Sub
all in one line in tbox1!!!!
I need
MIA_STRINGA
MIA_STRINGA
MIA_STRINGA
...
MIA_STRINGA

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reading text file do until...

Post by HansV »

You have to set the MultiLine property of SCROLL_OP to True!
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: Reading text file do until...

Post by sal21 »

HansV wrote:You have to set the MultiLine property of SCROLL_OP to True!
sorry for delay...
Resolved!