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

how do i remove a specific text from a file

Status
Not open for further replies.

tumor

Programmer
May 2, 2002
7
0
0
US
How do i remove a specific text/line from a text file without deleting the hole file.

i need the input CStr(Target) to be the specific text
like say that CStr(Target) = power
and i want to remove from the file user.txt
test
power
testing
powerfull

would become
file user.txt
test
testing
powerfull
 
Use the File System Object to read in the entire file. Delete the file from disk. Use the replace function to remove the undesired string(s). Write the file back to disk. Jon Hawkins
 
yes i figured that out but HOW i cant get to do anything but read the file. could some one plz post some code
 
If Left(command, 9) = "unnickban" Then
Target = ""
Target = Mid(commandU, 11, Len(commandU))
If colUsers.Online(CStr(Target)) And Not colUsers.ItemByName(CStr(Target)).bOperator Then

Set fso = CreateObject("Scripting.FileSystemObject")

' Read the contents of the file.
Set ta = fso_OpenTextFile("c:\testfile.txt", ForReading)
s = ta.ReadLine
ta.Close
If s = CStr(Target) Then
Set f1 = fso.CreateTextFile("c:\program files\direct connect hub\unbanned.txt", True)
' Write a line.
f1.WriteLine (CStr(Target))
f1.Close
Else
Set b = fso.CreateTextFile("C:\Program Files\Direct Connect Hub\banned.txt", True)
b.WriteLine(ta)
b.Close
End If

i know thats not the code but i think im close can some one tell me whats wrong
 
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile("C:\test.txt",1)
sText = oTS.ReadAll()
oTS.Close
sText = Replace(sText,"is","",1,-1,1)
Set oTS = oFSO.OpenTextFile("C:\test.txt",2)
oTS.Write sText
oTS.Close


If C:\test.txt begins like this:

This
is
a
test

it ends up like this:

Th

a
test Jon Hawkins
 
thank you very much but is there a way to keep it from removeing the "is" from "this".

also is the a command to remove blank lines
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top