[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

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?
0
Darkejo1
Asked:
Darkejo1
  • 4
  • 3
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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
 
Darkejo1Author Commented:
That did the trick! Thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now