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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove lines in a text file 2

Status
Not open for further replies.

brian32

Vendor
Mar 20, 2005
35
0
0
US
Hi. I need to remove the lines of a text file if it contains a certain word. For example, if the line contains the word "apples", then I want that line deleted.

The below code only copies the unwanted lines to another text file. But I want to completely remove the lines. Either that, or copy everything else over, WITHOUT the lines containing "apples". How do I modify the code below to accomplish this? Thanks.


Code:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Input.txt", ForReading)

Do Until objTextFile.AtEndOfStream
    strLine = objTextFile.ReadLine
    intFailure = InStr(strLine, "apples")
    If intFailure > 0 Then
        strNewText = strNewText & strLine & vbCrLf
    End If
Loop

objTextFile.Close
Set objTextFile = objFSO.OpenTextFile("C:\Output.txt", ForWriting, True)
objTextFile.Write(strNewText)
objTextFile.Close
 
Haven't tested it, but maybe something like this.

Code:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Input.txt", ForReading)
arrTemp = Split(objTextFile.ReadAll, VbCrLf)
objTextFile.Close
Set objTextFile = objFSO.OpenTextFile("C:\Input.txt", ForWriting)
strKeyWord = "apples"

For i = 0 To UBound(arrTemp)
	If Not InStr(arrTemp(i), strKeyWord) Then
		objTextFile.WriteLine arrTemp(i)
	End If
Next

objTextFile.Close

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
The script ran all the way through, but there were no changes. The file still contains the same information as the original. I'm examining it now, but not sure where the bottleneck is.
 
Maybe case difference?

try:
Code:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Input.txt", ForReading)
arrTemp = Split(objTextFile.ReadAll, VbCrLf)
objTextFile.Close
Set objTextFile = objFSO.OpenTextFile("C:\Input.txt", ForWriting)
strKeyWord = "APPLES"

For i = 0 To UBound(arrTemp)
	If Not InStr(UCase(arrTemp(i)), strKeyWord) Then
		objTextFile.WriteLine arrTemp(i)
	End If
Next

objTextFile.Close

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Still getting the same information returned. Just curious, what does the UCase function have to do with the script? I didn't think case sensitivity was relevant since apples is all in lower case.

It was suggested in another forum to put an Else clause into the If-Then block that will build a string of the good lines then write it out to a file at the end.

But I wasn't sure how to differenciate between the lines I want and the lines I don't want. All I know is I don't want the lines containing the word apples.
 
Don't worry. dm4ever will see that suggestion. :)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Are you both moderators for the vbscript forums? Sometimes Tektips goes down for some reason, I need to cross post to other forums.

Anyway, thanks to both of you for the input. Here's the final code:

Code:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Input.txt", ForReading)

Do Until objTextFile.AtEndOfStream
    strLine = objTextFile.ReadLine
    intFailure = InStr(strLine, "apples")
    If intFailure > 0 Then
        strNewText = strNewText & strLine & vbCrLf
    Else
        strOtherNewText = strOtherNewText & strLine & vbCrLf
    End If
Loop

objTextFile.Close
Set objTextFile = objFSO.OpenTextFile("C:\Output1.txt", ForWriting, True)
objTextFile.Write(strNewText)
Set objTextFile = objFSO.OpenTextFile("C:\Output2.txt", ForWriting, True)
objTextFile.Write(strOtherNewText)
objTextFile.Close
 
I'm glad you found a solution. Normally I would test what I post...should have done it this time. Anyway, I'm not a moderator...just a guy wondering around looking for things to distract me or teach me something new.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
It's the Not InStr part of the code that's the problem. It is always Not because InStr returns an integer rather than a boolean. You just need to change the code to:
Code:
If InStr(UCase(arrTemp(i)), strKeyWord) = 0 Then

You can use this code to see how Not does not work with InStr:
Code:
If Not InStr("abc","a") Then
  WScript.Echo "Not InStr: a is not in abc"
Else
  WScript.Echo "Not InStr: a is in abc"
End If
If InStr("abc","a") = 0 Then
  WScript.Echo "InStr = 0: a is not in abc"
Else
  WScript.Echo "InStr = 0: a is in abc"
End If
 
Thanks Skie, I actually figured that out when I took the time to look at what I had suggested.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Code:
Dim arrTemp(5)

arrTemp(0) = "some lines"
arrTemp(1) = "apples"
arrTemp(2) = "some other lines"
arrTemp(3) = "apples"
arrTemp(4) = "yet another line"
arrTemp(5) = "apples"

strKeyWord = "apples"

For i = 0 To UBound(arrTemp)
	If InStr(arrTemp(i), strKeyword) Then 
' 		objTextFile.WriteLine arrTemp(i)
		WScript.Echo "Match:     " & arrTemp(i)
	Else 
		WScript.Echo "No Match!: " & arrTemp(i)
	End If
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top