Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find (search)word in TXT file - extract line in variable - trim variable from left to filepath star

Status
Not open for further replies.

lklo

Industrial
Nov 24, 2010
226
Hi
Can anyone here explain me , how I can search i a TXT file (VB.net) ,
I know how to open with streamreader and so on , also to write or append to it.

But I need a code to search in file, for example at XTB or ZBTFG1102, and then store the entire line in a variable.
and next trim the variable until filepath start: U:\......
So the variable only contains the filepath.

Can anyone help me ?

lkl



txtfile:

7006.01.008.01 xtb -/20100377 U:\ugi\Arda\ADH-Diverse\Gru7008\gr03\Styr+Bosning\7002.07.001.01_teg.prt
7006.01.004.02 larsklok -/20100347 U:\ugi\Arda\ADH-Diverse\Gru7008\gr03\Styr+Bosning\7005.04.001.01_teg.prt
7006.01.004.01 zbtf1201 -/20111107 U:\ugi\Arda\ADH-Diverse\Gru7008\gr03\Styr+Bosning\7001.04.001.01_teg.prt
7008.03.002.01 ZNNOFGHOQAREGV -/20120301 U:\ugi\ArdA\ADH-Diverse\Gru7008\gr03\Styr+Bosning\7008.03.002.01_teg.prt
7008.03.001.01 ZBTFG1102 /20120307 U:\ugi\Arda\ADH-Diverse\Gru7008\gr03\Styr+Bosning\7008.03.001.01_teg.prt
 
Replies continue below

Recommended for you

Hi lklo

I wrote the below script to step through a csv file and change some expressions:


Code:
' NX 6.0.3.6

Option Strict Off
Imports System
Imports NXOpen
Imports System.IO
Imports System.Windows.Forms

Module NXJournal


Sub Main

Dim listFile as String
'open csv file
Dim fdlg As OpenFileDialog = New OpenFileDialog()
fdlg.Title = "Choose the Border CSV file"
fdlg.InitialDirectory = "C:\ESS\Border Change"
fdlg.Filter = "All files (*.*)|*.*|CSV files (*.csv)|*.csv"
fdlg.FilterIndex = 2
fdlg.RestoreDirectory = True
If fdlg.ShowDialog() = DialogResult.OK Then
listFile = fdlg.FileName
End If

'read the list into an array
Dim num_list As Long

Dim itemID As String
Dim itemRev As String


Dim tmpstream As StreamReader = File.OpenText(listFile)
Dim strlines() As String
Dim strline() As String

'Load content of file to strLines array
strlines = tmpstream.ReadToEnd().Split(Environment.NewLine)

' Count number of list
num_list = UBound(strlines)
Dim i As Integer


for i = 0 to num_list - 1
strline = strlines(i).Split(",")


itemID = strline(1)
itemRev = strline(2)


' Thing to do: START

Dim theSession As Session = Session.GetSession()

Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
theSession.ListingWindow.WriteLine("Number of lines: " + num_list.ToString + " Working on: " + i.ToString + " ")
theSession.ListingWindow.WriteLine("Opening: " + itemID.ToString + " Rev: " + itemRev + " ")

Dim basePart As BasePart
Dim partLoadStatus1 As PartLoadStatus
basePart = theSession.Parts.OpenBaseDisplay("@DB/" + itemID + "/" + itemRev, partLoadStatus1)


Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display


partLoadStatus1.Dispose()

Dim markId5 As Session.UndoMarkId
markId5 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression")

Dim expModified1 As Boolean
Dim errorMessages1() As String
workPart.Expressions.ImportFromFile("C:\ESS\Border Change\exp_to_change.exp", ExpressionCollection.ImportMode.Replace, expModified1, errorMessages1)

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId5)



Dim partSaveStatus1 As PartSaveStatus
partSaveStatus1 = workPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.True)

theSession = Nothing
basePart = Nothing
partLoadStatus1 = Nothing
workPart = Nothing
displayPart = Nothing
markId5 = Nothing
expModified1 = Nothing
errorMessages1 = Nothing
nErrs1 = Nothing
partSaveStatus1.Dispose()


' Things to do: END

lw.WriteLine("Done processing")
lw.WriteLine(" ")
lw.Close()

next

End Sub
End Module

The CVS file looks like:

Code:
,ItemID1,ItemRev,;
,ItemID2,ItemRev,;
,ItemID3,ItemRev,;

Adam
 
HI Adam

thank you for the answer , I´m not sure if I can use it ,but
I will give it a try

lklo

 
hi again -

I tryed with this solution - it work´s very well.

attached code = test

I have to use the search function in a menu with blockstyler.

lklo


Code:
Option Strict Off
Imports System
Imports NXOpen
Imports System.IO
Imports System.Environment
'Imports System.Windows.Forms
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.UF

Module NXJournal
Sub Main


	   Dim nxs As Session = Session.GetSession()'-------------------------------
	   Dim lw As ListingWindow = nxs.ListingWindow'-----------------------------

        Dim path As String = "C:\NX_test_dir\G_h_Map_File\G-h_List.txt" 
        Dim sw As StreamWriter

'====================================================================================
       Dim objFile As StreamReader = File.OpenText(path)

	 dim  strLine as string
	 dim hole_line as string
	 dim trim_line_left as string
	 dim trim_line_right as string
	 dim final_path as string
	 dim first_pos as integer
	 dim last_pos as integer
	 dim find_word as string = "Zbtf1"

    Do While objFile.Peek() >= 0

	strLine = objFile.ReadLine()

		If InStr(1,strLine, find_word,vbTextCompare) Then
		first_pos =InStr(1,strLine, ":\",vbTextCompare)
		last_pos =InStr(1,strLine, ".prt",vbTextCompare)
		hole_line=strLine

		End If

        Loop

	 objFile.Close()

	trim_line_left=mid(hole_line,(first_pos-1))
	trim_line_right=left(trim_line_left,(+last_pos-first_pos+6))
	final_path=trim_line_right


	lw.Open()'---------------------------------------------------------------

   		lw.WriteLine("test Search:>>" & find_word)
		lw.WriteLine("test Catched strLine:>>" & hole_line)
		lw.WriteLine("test final_path>>" & final_path)
'========================================================================================
End Sub
End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor