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

Author Message
stevemarks59

  • Total Posts : 5
  • Scores: 0
  • Reward points : 0
  • Joined: 4/8/2010
  • Status: offline
VBScript to remove (delete) string from a text file. Sunday, January 22, 2012 1:19 AM (permalink)
0
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
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  
 

<message edited by stevemarks59 on Sunday, January 22, 2012 1:25 AM>
 
#1
    59cobalt

    • Total Posts : 981
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:VBScript to remove (delete) string from a text file. Sunday, January 22, 2012 6:30 AM (permalink)
    0
    Deleting a particular string is the same as replacing it with the empty string (""), so there's no need to change the script.

    If you want to change the script anyway, change these two lines
    ReplaceWith  = WScript.Arguments(1)
    FileName     = WScript.Arguments(2)
    into this:
    ReplaceWith  = ""
    FileName     = WScript.Arguments(1)

     
    #2
      stevemarks59

      • Total Posts : 5
      • Scores: 0
      • Reward points : 0
      • Joined: 4/8/2010
      • Status: offline
      Re:VBScript to remove (delete) string from a text file. Sunday, January 22, 2012 5:35 PM (permalink)
      0
      Thanks the "" in the command line works great!
       
       replace.vbs OLDSTRING "" C:\file.txt 

       
      #3
        59cobalt

        • Total Posts : 981
        • Scores: 91
        • Reward points : 0
        • Joined: 7/17/2011
        • Status: offline
        Re:VBScript to remove (delete) string from a text file. Sunday, January 22, 2012 8:08 PM (permalink)
        0
        You're welcome.
         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9