Home > Back-end >  Code to get the line where a word is placed on a txt file
Code to get the line where a word is placed on a txt file

Time:11-03

I'm unable to write a code that looks for a word in a txt file and return the line where the word is placed.

The word is usually at the 31th line (placed at the start of the string) but sometimes is placed below. I tried to make a loop that stopped at the 32th line and it worked, but when the string is placed on a different number line the code doesn't work.

That's because I thought on looking for the word and then getting the line.

I've tried some copy-paste codes from many places but I couldn't managed to make them work.

Here is a sample of the *.txt file:

%%%
  VERSION:1
  LANGUAGE:ENGLISH
%%%

MODULE RP02


PROC P02()
ConfL\Off;
ConfJ\Off;
vfeed.v_tcp := 20;
vfeed.v_ori := 300;
vrapid.v_tcp := 500;
vrapid.v_ori := 300;
vfeed.v_leax := 500;
vfeed.v_reax := 20;
vrapid.v_leax := 1000;
vrapid.v_reax := 20;
CurrZone.pzone_tcp :=1;
CurrZone.pzone_ori :=2;
CurrZone.pzone_eax := 100;
CurrZone.zone_leax := 30;
CurrZone.zone_reax := 30;
AccSet 10,50;

CurrWObj := caja1;
CurrWObj.oframe.trans := [0,0,0];
CurrWObj.oframe.rot := [1,0,0,0];
! Item ID 2.1: NCL Code - Carga herramienta_02_D63_L520_H12_CAR=1

LoadTool 2,H12_PAT_1,1,1;
! Item ID 2.2: NCL Code - Posicionamiento ST180 R
MoveAbsJ [[96.98,-14.64,15.98,87.92,-32.69,-87.52],[2040,0,0,0,0,0]],vrapid,CurrZone,CurrTool;
MoveAbsJ [[96.98,-14.64,15.98,87.92,-32.69,-87.52],[2540,0,0,0,0,0]],vrapid,CurrZone,CurrTool;
!---------------------------------------------

And this is the code that worked previously:

Dim fso As New FileSystemObject
Dim f As File
Dim fsoStream As TextStream
Dim strLine As String
Dim j As Integer

Set f = fso.GetFile("C:\fileToRead.txt")
Set fsoStream = f.OpenAsTextStream(ForReading)

j = 1

Do Until j > 32


strLine = fsoStream.ReadLine
Debug.Print strLine

j = j   1

Loop

But when the string where "LoadTool" is placed changes the line number this doesn't work...

I tried to modify copy/pastes I found on the web in order to achieve a FIND function or something similar and get the line number but I got stucked...

CodePudding user response:

I think the problem is that you stop at line 32. Just run the loop Until fsoStream.AtEndOfStream and check each line for your SearchWord. If found then exit the loop.

Option Explicit

Sub Example()
    Dim fso As New FileSystemObject
    
    Dim f As Object
    Set f = fso.GetFile("C:\fileToRead.txt")
    
    Dim fsoStream As TextStream
    Set fsoStream = f.OpenAsTextStream(ForReading)
    
    Dim SearchWord As String
    SearchWord = "LoadTool"
    
    Dim strLine As String
    Do Until fsoStream.AtEndOfStream
        strLine = fsoStream.ReadLine
        Debug.Print strLine
        
        If Left$(strLine, Len(SearchWord)) = SearchWord Then
            MsgBox "'" & SearchWord & "' was found."
            Exit Do
        End If
    Loop
    
    'strLine now contains
    'LoadTool 2,H12_PAT_1,1,1;
    
End Sub
  •  Tags:  
  • vba
  • Related