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.
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

I have started with this, but it is not working for me.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = True
Dim objFSO, objFile, strNextLine, strLine

 If objFSO.FileExists("C:\scripts\test.txt") then
    Set objFile = objFSO.OpenTextFile("C:\scripts\test.txt", 1)

    Do Until objFile.AtEndOfStream
      strLine = objTextFile.Readline
      If strNextLine = "M8010 (Tool Head DOWN)" then
            strLine = strLine + "More lines of text"
      End If

      strNewText = strLine + vbcrlf
  Loop
  Set objFile = Nothing

  Set objFile = objFSO.OpenTextFile("C:\scripts\test.txt", 2)
  objFile.Write strNewText
  Set objFile = Nothing
End If
 
What about this ?
Code:
Const ForReading = 1, ForWriting = 2
strSearch = "M8010 (Tool Head DOWN)"
strAdd = "N0109 (Park Tool)" & vbCrLf _
       & "N0110 G01 F999.900" & vbCrLf _
       & "N0111 X-1.5 Y1.5 Z-2.82"
strFile = "C:\scripts\test.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFile) Then
    Set objFile = objFSO.OpenTextFile(strFile, ForReading)
    Do Until objFile.AtEndOfStream
        strLine = objTextFile.ReadLine
        If strLine = strSearch Then
            strLine = strLine & vbCrLf & strAdd
        End If
        strNewtext = strNewtext & strLine & vbCrLf
    Loop
    objFile.Close
    Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
    objFile.Write strNewtext
    objFile.Close
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV ... I get an error "Object Required: objTextFile
 
OOps, sorry, didn't notice that.
Replace this:
strLine = objTextFile.ReadLine
with this:
strLine = objFile.ReadLine

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I think I found my original type-o Should be objFile -NOT- objTextFile
 
We both saw that.... I think its good now. Thanks for your help! I will test this out.
 
PHV,

Can I search for a partial line to get the same results? My search line starts with M8010, but can change to other numbers. The most important is the (Tool Head DOWN).
 
Code:
Const ForReading = 1, ForWriting = 2
[!]strSearch = "(Tool Head DOWN)"[/!]
strAdd = "N0109 (Park Tool)" & vbCrLf _
       & "N0110 G01 F999.900" & vbCrLf _
       & "N0111 X-1.5 Y1.5 Z-2.82"
strFile = "C:\scripts\test.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFile) Then
    Set objFile = objFSO.OpenTextFile(strFile, ForReading)
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        [!]If InStr(strLine, strSearch, 1) Then[/!]
            strLine = strLine & vbCrLf & strAdd
        End If
        strNewtext = strNewtext & strLine & vbCrLf
    Loop
    objFile.Close
    Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
    objFile.Write strNewtext
    objFile.Close
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

When I run this code, I get a "type mismatch" error on this new line of code:

Code:
If InStr(strLine, strSearch, 1) Then

I don't see any errors myself but I will continue to look.
 
I am no expert... but I changed to :

Code:
If InStr(strLine, strSearch) Then

Removing the (, 1) seems to have done it.
 
The code works great, so I am trying to move on to the next part. I have this HTA that I got here awhile back for comparing files. I don't have a great understanding of HTA and how code is executed within. I placed this code in this HTA and I want to try and browse for the file to execute this code on, but I think I could be way off. Down near the bottom for "On button click", I am not sure how the code is executed.

Code:
<html>
<head>
<Title>Compare Files / Output Changes</Title>
<HTA:APPLICATION

 
APPLICATIONNAME="File2Compare"
ID="File2Compare"
BORDER="dialog"
INNERBORDER="no"
MAXIMIZEBUTTON="no"
SCROLL="no"
VERSION="1.0"/>

<style type="text/css">


 
input,button

  {

 border:1px solid #FF6A22;
 background-color:lightblue;
 font-family:Courier;

 font-size:16px;
   color:#0000DD;
      font-weight:; 
                     }

 
textarea{background-color:lightgray;
Color:black;}

 
 body { 

   overflow:auto;
     position:relative;
   
   top:5px;
    left:5px;

scrollbar-face-color: #DEE3E7;
scrollbar-highlight-color: #FFFFFF;
scrollbar-shadow-color: #DEE3E7;
scrollbar-3dlight-color: #D1D7DC;
scrollbar-arrow-color:  #006699;
scrollbar-track-color: #EFEFEF;
scrollbar-darkshadow-color: #98AAB1; 
background-color:black;
margin-top:6px;

color:none;

filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='lightblue', EndColorStr='blue'); 

 }

 
button  {       
border:18px solid #0000DD;
 background-color:#0000DD;
  font-family:Verdana;
   font-size:18px;
     height:16px;
      font-weight:bold; 
        cursor:hand;    }

 
div.forumline {margin-left:10px; background-color: black; border: 4px #006699 solid; float:left; 
height:600px; width:650px; overflow:scroll;  }
div.form {float:left;font-family:Arial;} 
div.toolbar {width:145%; font-family:Arial;}

 
th,td,p { font-family: Verdana, Arial, Helvetica, sans-serif }

  hr      { height: 8px; border: solid #D1D7DC 8px; border-top-width: 1px;}

.gen { font-size : 16px; }

.genmed { font-size : 16px; }

.gensmall { font-size : 16px; }

.gen,.genmed,.gensmall { color : #000000; }

a.gen,a.genmed,a.gensmall { color: #006699; text-decoration: none; }

a.gen:hover,a.genmed:hover,a.gensmall:hover     { color: #DD6900; text-decoration: underline; }

input.button {  background-color : #0000DD;
                color : #000000; cursor:hand;
                font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; }

 
input.longbt {  background-color : #0000DD;
                color : #000000;
                font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; width:140px}

 
.code        { font-family: Courier, 'Courier New', sans-serif; font-size: 11px; color: #006600;
               background-color: #FAFAFA; border: #D1D7DC; border-style: solid;

         border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px }

.quote {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #444444; line-height: 145%;

         background-color: #FAFAFA; border: #D1D7DC; border-style: solid;
         border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px }

.err{color: white; background-color: black; font-family: Verdana, Arial, Helvetica, sans-serif }

#FolderPath{color:#FF0000}

 
</style>
</head>
<script language="VBScript">

Function File2Compare(strFile)
Const ForReading = 1, ForWriting = 2

Dim objFSO,objSourceFile,objSourceFile2
Dim intLineNum, intLineNum2

Title ="File to Compare" 



strSearch = "(Tool Head DOWN)"

strAdd = "N0109 (Park Tool)" & vbCrLf _
       & "N0110 G01 F999.900" & vbCrLf _
       & "N0111 X-1.5 Y1.5 Z-2.82"
       
strFile = "C:\scripts\test.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(strFile) Then
    Set objFile = objFSO.OpenTextFile(strFile, 1)
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        
        If InStr(strLine, strSearch) 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:  " & strFile, vbInformation
End If
End Function

Sub Window_OnLoad

CenterWindow 820, 680

End Sub

 
Sub CenterWindow(x,y)

window.resizeTo x, y

iLeft = window.screen.availWidth/2 - x/2
itop = window.screen.availHeight/2 - y/2

window.moveTo ileft, itop

End Sub

 
Sub OnClickButtonOK()

window.Close

End Sub

 
Sub OnClickButtonCancel()

window.Close

End Sub

 
</script>
<center>

<FONT face="Courier" color="black" size=2>Compare File #1:</FONT><input type="file" size="45" name="file1" id="file1" /><br>

<FONT face="Courier" color="black" size=2>Compare File #2:</FONT><input type="file" size="45" name="file2" id="file2" /><br>

<FONT face="Courier" color="black" size=2>Results of Comparison:</FONT><br/>

<textarea id="txtBody" rows="23" cols="65"></textarea><br><br>

<input type="button" style="width:165;height:65" name="OK" id="OK" value="COMPARE FILES" onclick="strFile">

<input type="button" style="width:165;height:65" name="Cancel" id="Cancel" value="EXIT PROGRAM" onclick="OnClickButtonCancel">

</td></tr>
</table>
</table>
</body>
</html>
 
Try:
<input type="button" style="width:165;height:65" name="OK" id="OK" value="COMPARE FILES" onclick="[highlight #FCE94F]File2Compare(file1.value)[/highlight]">
 
Thanks Guitarzan, that did the trick.

Now what I want to do is basically run this code twice, but what I have tried has given me strange results like tripling the text lines in my file.

What I want to do is add another search like:

Code:
strSearch2 = "(Tool Head UP)"

And then add the same text as my strAdd. Any help would be great!
 
Code:
Function File2Compare(strFile)
Const ForReading = 1, ForWriting = 2

Dim objFSO,objSourceFile,objSourceFile2
Dim intLineNum, intLineNum2

Title ="File to Compare" 



strSearch = "(Tool Head DOWN)"
[highlight #FCE94F]strSearch2 = "(Tool Head UP)"[/highlight]

strAdd = "N0109 (Park Tool)" & vbCrLf _
       & "N0110 G01 F999.900" & vbCrLf _
       & "N0111 X-1.5 Y1.5 Z-2.82"
       
strFile = "C:\scripts\test.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(strFile) Then
    Set objFile = objFSO.OpenTextFile(strFile, 1)
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        
        If InStr(strLine, strSearch) [highlight #FCE94F]or InStr(strLine, strSearch2)[/highlight] 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:  " & strFile, vbInformation
End If
End Function
 
Wow, that was easy. I tried something similar but with "AND" and it did not like that.

My next and hopefully last is, I want to prevent the file from getting this update if it already has it. So I am searching for a keyword which is in my strAdd (Park Tool). If it finds this, then quit the script, but when I try this it does not work.

Code:
If InStr(strLine, strKey) Then 
MsgBox "No Update Required"
End If
wscript.quit
 
Not tested:
Code:
Function File2Compare(strFile)
Const ForReading = 1, ForWriting = 2

Dim objFSO,objSourceFile,objSourceFile2
Dim intLineNum, intLineNum2

Title ="File to Compare" 



strSearch = "(Tool Head DOWN)"
strSearch2 = "(Tool Head UP)"
[highlight #8AE234]strKey = "(Park Tool)"[/highlight]

strAdd = "N0109 (Park Tool)" & vbCrLf _
       & "N0110 G01 F999.900" & vbCrLf _
       & "N0111 X-1.5 Y1.5 Z-2.82"
       
strFile = "C:\scripts\test.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(strFile) Then
    [highlight #8AE234]If InStr(objFSO.ReadAll, strKey) Then[/highlight] 
        [highlight #8AE234]MsgBox "No Update Required"[/highlight]
    [highlight #8AE234]Else[/highlight]
        Set objFile = objFSO.OpenTextFile(strFile, 1)
        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:  " & strFile, vbInformation
    [highlight #8AE234]End If[/highlight]
End If
End Function
 
I am getting an error on that line "Object doesn't support this property or method (objFSO.ReadAll)"

Thanks for your help!
 
guitarzan, I'd replace this:
objFSO.ReadAll
with this:
objFSO.OpenTextFile(strFile, 1).ReadAll

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top