4 Replies - 1055 Views - Last Post: 06 May 2018 - 06:05 AM Rate Topic: -----

#1 JavaThoughts   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 10-April 18

How to count specific lines and column with Microsoft ScriptingRuntime

Posted 03 May 2018 - 01:11 AM

Greetings,

I am using VB6 and Microsoft Scripting Runtime. I'm trying to count the number of columns and lines/rows starting from a specific part. I want to start counting from below "MapBody{" and stop counting right after "}" but my rows keep appearing as 0. Also, how do I count the columns for only the same part which is below "MapBody{" and above "}"? Below is the text file that I am working on using VB6:

Mapping file = 4262970.111
Notch Side = 0
MapBody{
@...21.........
...1112........
...111111......
.112111111.....
..1111111......
....1111.......
}



My code :

Option Explicit

Private Sub cmdRead_Click()
    Dim ts As TextStream
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    
    Dim sInput As String
    Dim pInput
    Dim tInput As String
    Dim sStart As String
    Dim sEnd As String
    Dim sCount As Integer

 Set ts = fso.OpenTextFile(txtFilePath.Text)
    ts.ReadAll
    
    Do While Not ts.AtEndOfStream
        sInput = ts.ReadLine 
        If sInput = "MapBody{" Then
            Do Until sInput = "}"
                sCount = ts.Line
            Loop
        End If
    Loop
    
    tInput = "Total rows : " + CStr(sCount)
    
    MsgBox "Text file created in desktop." + vbCrLf + vbCrLf + tInput, , "Creating file"
    
End Sub




Please help, thanks in advance...

Is This A Good Question/Topic? 0
  • +

Replies To: How to count specific lines and column with Microsoft ScriptingRuntime

#2 andrewsw   User is offline

  • head thrashing
  • member icon

Reputation: 6667
  • View blog
  • Posts: 27,299
  • Joined: 12-December 12

Re: How to count specific lines and column with Microsoft ScriptingRuntime

Posted 03 May 2018 - 04:24 AM

sCount is an integer so should not be assigned a string. Presumably you intended to name it iCount to make this clear.

You should initialise it to zero then increment it by one each time to achieve counting.
Was This Post Helpful? 0
  • +
  • -

#3 JavaThoughts   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 10-April 18

Re: How to count specific lines and column with Microsoft ScriptingRuntime

Posted 03 May 2018 - 08:02 AM

View Postandrewsw, on 03 May 2018 - 04:24 AM, said:

sCount is an integer so should not be assigned a string. Presumably you intended to name it iCount to make this clear.

You should initialise it to zero then increment it by one each time to achieve counting.



Thank you but bear with me. It's my first programming language. I will change to iCount later on. I changed it to increment to 1 but my total rows is still zero. I have tried putting sCount = 0 but the rows still remain as zero.

My code :

Do While Not ts.AtEndOfStream
        sInput = ts.ReadLine
        
        If sInput = "MapBody{" Then
            Do Until sInput = "}"
                
                sCount = sCount + 1
                
            Loop
        End If
    Loop

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • head thrashing
  • member icon

Reputation: 6667
  • View blog
  • Posts: 27,299
  • Joined: 12-December 12

Re: How to count specific lines and column with Microsoft ScriptingRuntime

Posted 04 May 2018 - 01:16 AM

Is the if condition ever met? Put a MsgBox inside the if to confirm that the condition is met at some point. Or delve into debugging and step through your code.

Your do loop does not continue to read lines from the file, so I would have thought that, when begun, it would spin endlessly. That is, as it does not continue through the file, the value of sInput will remain as "MapBody{".
Was This Post Helpful? 1
  • +
  • -

#5 JavaThoughts   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 10-April 18

Re: How to count specific lines and column with Microsoft ScriptingRuntime

Posted 06 May 2018 - 06:05 AM

Yes! Thank you! It's working now. You're right, the do loop was not reading the lines. I changed it a little and added in the ts.AtEndOfStream as well as the ts.ReadLine in between. Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1