# Remove lines in txt file with vbs



## cbanks (Mar 23, 2011)

The code below will remove lines that contain the letter "i". How do I change the code so that it will only remove lines that contain the word "i".

Example lines:
I went to the store.
You are nice.

I want the code to remove the first line, but not the second line. Any ideas? Thanks.



Code:

Option Explicit

dim filepath, VBSName, objFSO, objTextFile, objTextFile2
dim fs, folder, file, item, line, fs1, filename

VBSname = wscript.scriptfullname

Filepath = left(VBSname, Len(VBSname) -(len(VBSname) - InstrRev(VBSname, "\")))

set fs = CreateObject("Scripting.FileSystemObject")
set folder = fs.GetFolder(FilePath)

For each item in folder.Files
If Right(item.name, 3) = "txt" Then

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (item, 1)
Set fs1 = objFSO.CreateTextFile ("temp.txt",True)
fs1.close
Set objTextFile2 = objFSO.OpenTextFile ("temp.txt", 8)

Do Until objTextFile.AtEndOfStream
Line = objTextFile.ReadLine

If InStr(1,Line, "i",1) Then

else
objTextFile2.writeline line
End If
Loop

set objTextFile = nothing
set objTextFile2 = nothing

filename = item.name

objFSO.CopyFile "temp.txt", filename, true

objFSO.DeleteFile "temp.txt"

set objFSO = nothing
Set fs1 = nothing

End If
next

msgbox "Done!"


----------



## jiml8 (Jul 3, 2005)

I am not totally sure of the syntax but this is what you want.

If (InStr(1,Line, " i ",1) or InStr(1,Line,"i ",1) or InStr(1,Line," i\n",1)) Then

In this, \n is the newline symbol; I'm not sure what that is in VB since I haven't programmed VB for many years and never programmed it much.

As stated, this line will remove the i from a word that ends in i (such as Ravi) so you have to do a further test to identify this case and throw it out. You do this by testing if the i you have found is at the start of the line or not.


----------



## cbanks (Mar 23, 2011)

Can anyone else help me with the code required to remove lines that contain the word "i", without removing lines that have words that contain "i" within them? Thanks.


----------



## jiml8 (Jul 3, 2005)

So I take it you don't know how to read?

I'll respond no further on this thread.

Sheesh.


----------

