Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

Search for string in textfile and place text below it

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
I have new text files that I need to make updates to on a regular basis. What I need is to find a line of text and then append below that line three additional lines like this:

Code:
'Search for this line which has hundreds of lines below it
M8010 (Tool Head DOWN)

Once found, enter these lines directly below it:

Code:
N0109 (Park Tool)
N0110 G01 F999.900
N0111 X-1.5 Y1.5 Z-2.82

Thanks for any help or examples.
 
Here is my final piece of code, but the strKey search is causing an error. I'm stuck.

Code:
Function File2Compare

Dim objFSO, strFile

strFile = strFile2.Value

Const ForReading = 1, ForWriting = 2

Title ="File to Compare" 


strSearch = "(Tool Head DOWN)"
strSearch2 = "(Tool Head UP)"
strKey = "Park Tool"

strAdd = "N0109 (Park Nozzle)" & vbCrLf _
       & "N0110 G01 F999.900" & vbCrLf _
       & "N0111 X-1.5 Y1.5 Z-2.82"
       


Set objFSO = CreateObject("Scripting.FileSystemObject")


   [COLOR=#CC0000]If InStr(objFSO.ReadAll, strKey) Then 
        MsgBox "No Update Required"
    Else[/color]    

    Set objFile = objFSO.OpenTextFile(strFile, 1).ReadAll
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        
		
		
        If InStr(strLine, strSearch) or InStr(strLine, strSearch2) Then
          strLine = strLine & vbCrLf & strAdd
		End If
        strNewtext = strNewtext & strLine & vbCrLf
		 
    Loop
    
    objFile.Close
    Set objFile = objFSO.OpenTextFile(strFile, 2)
    objFile.Write strNewtext
    objFile.Close
	MsgBox "File has been updated with Park Instructions" & vbCr & vbCr & strFile, vbInformation
End If
End Function
 
PHV corrected my mistake. Should have been:
Code:
If InStr(objFSO.OpenTextFile(strFile, 1).ReadAll, strKey) Then
 
Ah, now I see how it is written. Thank You all for the help and your time.... very much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top