Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

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

    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

  • #2
    Re: VBScript to remove (delete) string from a text file.

    How about replace it with an empty string ""
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: SOLVED

      I posted this question differently on another forum and received a reply which worked perfectly

      My question was answered by "PHV" on another forum.
      I am not allowed to post a link so I posted the text from the other post in case someone may be interested.

      The question I asked there was a bit different than what I
      asked here but the answer is applicable.



      My post on the other forum:
      --------------------------------------

      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
      ---------------------

      The reply which worked perfectly:
      --------------------
      New PostPHV (MIS)
      22 Jan 12 9:10
      What happens if you use this command line ?
      replace.vbs OLDSTRING "" C:\file.txt
      ----------------------------------

      Comment

      Working...
      X