Replace specified text in text file with nothing in .vbs.

What i'm trying to do is search through a .txt file and replace a specified line or delete it using .vbs. I know I need to open the file, read it to memory, then write the info into another file. Can someone show me how to do this?
Darkejo1Asked:
Who is Participating?
 
sirbountyCommented:
Ah - I see it now...
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
OriginalText = "10.144.39.100"
FileName = "C:\computerlist.txt"
 
Set objFile= objFSO.OpenTextFile(FileName)
newData = Replace(objFile.ReadAll, OriginalText, "")
objFile.Close
 
Set objFile = objFSO.CreateTextFile(FileName)
objFile.Write newData
objFile.Close

Open in new window

0
 
sirbountyCommented:

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
OrgiginalText = "ABCDEFG"
FileName = "C:\File.txt"
 
Set objFile= objFSO.OpenTextFile(FileName).ReadAll
newData = Replace(objFile.ReadAll, OriginalText, "")
objFile.Close
 
Set objFile = objFSO.CreateTextFile(FileName)
objFile.Write newData
objFile.Close

Open in new window

0
 
Patrick MatthewsCommented:
No points for this!  :)

Change:

Set objFile = objFSO.CreateTextFile(FileName)

to:

Set objFile = objFSO.CreateTextFile(FileName, True)
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
sirbountyCommented:
I'm not 100% certain that's necessary?  If it's a 'just in case' - then I probably agree with you, but if the file is closed, I've not had a problem overwriting it before...
0
 
Darkejo1Author Commented:
It wasn't needed. However it's not working. It tell sme "object Required". The data is in the text file. What does this mean?
strcomputer1 = Trim(strcomputer)
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
OriginalText = "10.144.39.100"
FileName = "C:\computerlist.txt"
 
Set objFile= objFSO.OpenTextFile(FileName).ReadAll
newData = Replace(objFile.ReadAll, OriginalText, "")
objFile.Close
 
Set objFile = objFSO.CreateTextFile(FileName)
objFile.Write newData
objFile.Close

Open in new window

untitled.bmp
0
 
sirbountyCommented:
Um - then something else is added here?  THere's only 12 lines in this script your error references line 98?
You running this in an HTA?
0
 
Darkejo1Author Commented:
Yes, This is only part of a larger peice of the puzzle. I've removed the rest and kept only your code. I still get the error message.
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
OriginalText = "10.144.39.100"
FileName = "C:\computerlist.txt"
 
Set objFile= objFSO.OpenTextFile(FileName).ReadAll
newData = Replace(objFile.ReadAll, OriginalText, "")
objFile.Close
 
Set objFile = objFSO.CreateTextFile(FileName)
objFile.Write newData
objFile.Close

Open in new window

untitled.bmp
0
 
Darkejo1Author Commented:
That did the trick! Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.