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!

VBScript to remove (delete) string from a text file.

Status
Not open for further replies.

stevemarks59

Technical User
Apr 8, 2010
22
US
I have the "replace.vbs" file saved to C:\Windows.
I use it to find and replace strings in text files.
Command line Syntax:

replace.vbs OLDSTRING NEWSTRING C:\file.txt


How can I modify the replace.vbs script to delete the string rather than replace it with another string?

I have very little scripting knowledge so I would really
appreciate help with this.

replace.vbs
Code:
Dim FileName, Find, ReplaceWith, FileContents, dFileContents
Find         = WScript.Arguments(0)
ReplaceWith  = WScript.Arguments(1)
FileName     = WScript.Arguments(2)

FileContents = GetFile(FileName)

dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)

if dFileContents <> FileContents Then
  WriteFile FileName, dFileContents
End If

function GetFile(FileName)
  If FileName<>"" Then
    Dim FS, FileStream
    Set FS = CreateObject("Scripting.FileSystemObject")
      on error resume Next
      Set FileStream = FS.OpenTextFile(FileName)
      GetFile = FileStream.ReadAll
  End If
End Function

function WriteFile(FileName, Contents)
  Dim OutStream, FS

  on error resume Next
  Set FS = CreateObject("Scripting.FileSystemObject")
    Set OutStream = FS.OpenTextFile(FileName, 2, True)
    OutStream.Write Contents
End Function
 
What happens if you use this command line ?
replace.vbs OLDSTRING "" C:\file.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
@PHV: Well I'll just tell you what happened
when I used the script you posted above in
a batch file and ran it...
IT WORKED ABSOLUTELY PERFECTLY!
THANK YOU SO VERY MUCH FOR YOUR HELP!


Batch File:
Code:
@ECHO OFF
replace.vbs custombuttons-button44, "" "C:\localstore.rdf"
exit

I could not figure out what to use for an
"empty string". I searched Google and
found nothing useful. The two double quotation
marks ("") in your command line syntax
sure did the trick.
Thanks again for taking the time to help me.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top