open text file and search for text

open text file and search for text

martinhoos
Advocate Advocate
1,515 Views
7 Replies
Message 1 of 8

open text file and search for text

martinhoos
Advocate
Advocate

Hello,

i dont know how to do... i like to find text in an text file.

The idea:  rund the rule and place there a text. the rule open the text file and search for the text.

is there anyone who can help me?

Regards Martin

0 Likes
1,516 Views
7 Replies
Replies (7)
Message 2 of 8

martinhoos
Advocate
Advocate

Like the pic:

 

find.jpg

0 Likes
Message 3 of 8

theo.bot
Collaborator
Collaborator

You can use the "System.IO.File.Readlines" to go thru the text. Here is a small sample

 

'____Open and read a text file line by line_______________________

Dim oFind As String
Dim Line As String
Dim I As Integer

oFind = InputBox("Enter text", "Search text", "")

If oFind <> "" Then
	For Each Line In System.IO.File.ReadLines("C:\Users\theob\Downloads\debug.log")

		If Line.Contains(oFind)Then
			Logger.Info("Text found at Line" &  I)

		End If
		
		I=I+1
	Next
Else
	MessageBox.Show("No search string provided")
End If

 

0 Likes
Message 4 of 8

martinhoos
Advocate
Advocate

Thank you for the reply...

 

i do not have many experience with ilogic

As i understand your code, the line Logger.Info will highlight the text.

 

I changed the the path:  

C:\Users\theob\Downloads\debug.log

to:

 

C:\Users\tb1\Desktop\user.log

But nothing will happend. 

 

After that i put in the line:

MessageBox.Show("Text found at Line" & I)

 i get the result:

 

Text found at Line574015

 

What is wrong?

 

0 Likes
Message 5 of 8

theo.bot
Collaborator
Collaborator

I'm not sure what you want to do next after you found the text in your document. So the code that i provided read every line and returns the linenumber. Your messagebox does the same. it simply returns the line number. But you can do a lot of other things, depending on what your goal is. You can take the full line text or a part of it and reuse it in a property or text object in inventor. So if you find the text what is your next step that you want to take.

0 Likes
Message 6 of 8

martinhoos
Advocate
Advocate

Thank you for the reply...

 

The next step ... mhhhh is it possible to get the path above? The beginning of the path is everytime the same -> V:\inventor-5

If you get the path is it possible to open the os (explorer) folder?

 

There is one restriction:  If the text found more than one time - a message shoult be up.

 

Thank you very much Theo

 

 

martinhoos_0-1636543778171.png

 

0 Likes
Message 7 of 8

theo.bot
Collaborator
Collaborator

This could be a start (only change the file path in the code):

 

'____Open and read a text file line by line_______________________

Dim oFind,oFile, Line, oPath As String
Dim i,j As Integer
Dim oFound As Boolean
j=0
oFound = False
oFind = InputBox("Enter text", "Search text", "V:\inventor-5")

'use hardcoded path 
oFile = "C:\Users\theob\Downloads\debug.log"

If oFind <> "" Then
	For Each Line In System.IO.File.ReadLines(oFile)

		If Line.Contains(oFind) And oFound = False Then
			oPath = Right(Line, Len(Line) -Line.IndexOf(oFind))
			Logger.Info("Text found at Line" & i)
			oFound = True
			j=1
		ElseIf Line.Contains(oFind) And oFound = True Then
			j = j + 1
			Logger.Info("Text found at Multiple Lines!")
		End If
		
		i=i+1
	Next
Else
	MessageBox.Show("No search string provided")
End If


If oFound = True And j=1 Then
	Process.Start("explorer.exe", oPath)
ElseIf oFound = True And j>1 Then
	MessageBox.Show("Found " & j & " lines containing the text: " & oFind)
Else
	MessageBox.Show("Text """ & oFind & """ not found in text.")
End If

  

0 Likes
Message 8 of 8

martinhoos
Advocate
Advocate

Theo thank you very much....

 

The user documents explorer opend NOT the explorer with the V:\ path

 

The V:\ path in the text file is different.... only the beginning is equal  ->  v:\1-inventor

 

The Text file is created with:  dir v:\inventor-5\*.* /s > user.txt

 

for example:

 

martinhoos_0-1636550297305.png

 

In the text file are thousands of entries

 

 

Thanks

 

0 Likes