Link to home
Start Free TrialLog in
Avatar of Darkejo1
Darkejo1

asked on

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?
Avatar of sirbounty
sirbounty
Flag of United States of America image


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

No points for this!  :)

Change:

Set objFile = objFSO.CreateTextFile(FileName)

to:

Set objFile = objFSO.CreateTextFile(FileName, True)
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...
Avatar of Darkejo1
Darkejo1

ASKER

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
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?
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
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did the trick! Thank you!