Delete Line Function

Author Message
TomRiddle

  • Total Posts : 620
  • Scores: 12
  • Reward points : 0
  • Joined: 2/7/2008
  • Location: Australia
  • Status: offline
Delete Line Function Saturday, August 16, 2008 12:28 AM (permalink)
5
'VBScript does not have a simple function for removing a line from a file.
'After doing a search and not finding anything decent, I wrote this function.

'Examples
option explicit

'DeleteLine "c:\test.txt", "qwerty", 0, 0
'If script finds text qwerty on any line in text file c:\text.txt it will delete that line

'DeleteLine "c:\test.txt", "", 17, 0
'Script deletes line 17 in text file c:\text.txt

DeleteLine "c:\test.txt", "qwerty", 17, 0
'If script finds text qwerty on line 17 in text file c:\text.txt it will delete that line



 Function DeleteLine(strFile, strKey, LineNumber, CheckCase)
 'DeleteLine Function by TomRiddle 2008
 
 'Remove line(s) containing text (strKey) from text file (strFile)
 'or
 'Remove line number from text file (strFile)
 'or
 'Remove line number if containing text (strKey) from text file (strFile)
 
 'Use strFile = "c:\file.txt" 	(Full path to text file)
 'Use strKey = "John Doe"     	(Lines containing this text string to be deleted)
 'Use strKey = ""             	(To not use keyword search)
 'Use LineNumber = "1"        	(Enter specific line number to delete)
 'Use LineNumber = "0"        	(To ignore line numbers)
 'Use CheckCase = "1"         	(For case sensitive search )
 'Use CheckCase = "0"         	(To ignore upper/lower case characters)
 
 
    Const ForReading=1:Const ForWriting=2
    Dim objFSO,objFile,Count,strLine,strLineCase,strNewFile
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    Set objFile=objFSO.OpenTextFile(strFile,ForReading)
    Do Until objFile.AtEndOfStream
       strLine=objFile.Readline
       If CheckCase=0 then strLineCase=ucase(strLine):strKey=ucase(strKey)
       If LineNumber=objFile.Line-1 or LineNumber=0 then
          If instr(strLine,strKey) or instr(strLineCase,strkey) or strKey="" then
             strNewFile=strNewFile
          Else
             strNewFile=strNewFile&strLine&vbcrlf
          End If
       Else
          strNewFile=strNewFile&strLine&vbcrlf
       End If
    Loop
    objFile.Close
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    Set objFile=objFSO.OpenTextFile(strFile,ForWriting) 
    objFile.Write strNewFile 
    objFile.Close 
 
 End Function
 


<message edited by TomRiddle on Saturday, August 16, 2008 9:32 PM>
 
#1
    dm_4ever

    • Total Posts : 3687
    • Scores: 82
    • Reward points : 0
    • Joined: 6/29/2006
    • Location: Orange County, California
    • Status: offline
    RE: Delete Line Function Saturday, August 16, 2008 5:38 AM (permalink)
    0
    You know that there is a Line property that shows you the line number right?  objFile.Line 
    Also, since this may come in useful to some....maybe add the ability have it be case-sensitive or not.
    <message edited by dm_4ever on Sunday, August 17, 2008 3:26 PM>
    dm_4ever

    My philosophy: K.I.S.S - Keep It Simple Stupid
    Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
    Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      TomRiddle

      • Total Posts : 620
      • Scores: 12
      • Reward points : 0
      • Joined: 2/7/2008
      • Location: Australia
      • Status: offline
      RE: Delete Line Function Saturday, August 16, 2008 9:33 PM (permalink)
      0
      Thanks for those tips dm_4ever, I updated the script with those suggestions

      Cheers
       
      #3
        kam321

        • Total Posts : 3
        • Scores: 0
        • Reward points : 0
        • Joined: 2/18/2009
        • Status: offline
        RE: Delete Line Function Thursday, February 19, 2009 3:54 AM (permalink)
        0
        can you repost the new code??
         
         
        thanks
         
        k
         
        #4
          TomRiddle

          • Total Posts : 620
          • Scores: 12
          • Reward points : 0
          • Joined: 2/7/2008
          • Location: Australia
          • Status: offline
          RE: Delete Line Function Thursday, February 19, 2009 10:48 AM (permalink)
          0
          The original code posted has the revisions suggested by dm_4ever.
          Not sure if you can see the revisions link or not but the previous version does not have the ability to check or ignore case and the line number was found by incrementing a counter instead of objFile.Line 
          Cheers,
           
          Let me know if you have any problems with it.  
           
          #5
            rogersundstrom

            • Total Posts : 2
            • Scores: 0
            • Reward points : 0
            • Joined: 4/22/2009
            • Status: offline
            RE: Delete Line Function Wednesday, April 22, 2009 3:50 AM (permalink)
            0
            Hi,
             
            I have problems with this script and can´t figure why.
            Nothing happens when I try to run it.
             
            I have copy the code and created a file called deleteline.vbs
            Then I run deleteline "c:\test.txt", "", 3, 0 in a CMD prompt
            but my test.txt file is intact.
            Even if I run deleteline and point it to a made-up file, the script runs without any error
             
            My OS are Microsoft Windows XP [Version 5.1.2600] SP2 and Microsoft Visual Basic 6.3
             
            /RS
             
            #6
              TomRiddle

              • Total Posts : 620
              • Scores: 12
              • Reward points : 0
              • Joined: 2/7/2008
              • Location: Australia
              • Status: offline
              RE: Delete Line Function Wednesday, April 22, 2009 11:31 AM (permalink)
              0
              You need to put the line that runs the function within the vbs file and then run the vbs file
               
              ie
               
              deleteline3.vbs
               DeleteLine "c:\test.txt", "", 3, 0
                
               Function DeleteLine(strFile, strKey, LineNumber, CheckCase)
               'DeleteLine Function by TomRiddle 2008
               'Remove line(s) containing text (strKey) from text file (strFile)
               'or
               'Remove line number from text file (strFile)
               'or
               'Remove line number if containing text (strKey) from text file (strFile)
               'Use strFile = "c:\file.txt"  (Full path to text file)
               'Use strKey = "John Doe"      (Lines containing this text string to be deleted)
               'Use strKey = ""              (To not use keyword search)
               'Use LineNumber = "1"         (Enter specific line number to delete)
               'Use LineNumber = "0"         (To ignore line numbers)
               'Use CheckCase = "1"          (For case sensitive search )
               'Use CheckCase = "0"          (To ignore upper/lower case characters)
               
                  Const ForReading=1:Const ForWriting=2
                  Dim objFSO,objFile,Count,strLine,strLineCase,strNewFile
                  Set objFSO=CreateObject("Scripting.FileSystemObject")
                  Set objFile=objFSO.OpenTextFile(strFile,ForReading)
                  Do Until objFile.AtEndOfStream
                     strLine=objFile.Readline
                     If CheckCase=0 then strLineCase=ucase(strLine):strKey=ucase(strKey)
                     If LineNumber=objFile.Line-1 or LineNumber=0 then
                        If instr(strLine,strKey) or instr(strLineCase,strkey) or strKey="" then
                           strNewFile=strNewFile
                        Else
                           strNewFile=strNewFile&strLine&vbcrlf
                        End If
                     Else
                        strNewFile=strNewFile&strLine&vbcrlf
                     End If
                  Loop
                  objFile.Close
                  Set objFSO=CreateObject("Scripting.FileSystemObject")
                  Set objFile=objFSO.OpenTextFile(strFile,ForWriting) 
                  objFile.Write strNewFile 
                  objFile.Close 
               End Function
               
               

               
              #7
                rogersundstrom

                • Total Posts : 2
                • Scores: 0
                • Reward points : 0
                • Joined: 4/22/2009
                • Status: offline
                RE: Delete Line Function Thursday, April 23, 2009 12:59 AM (permalink)
                0
                Fantastic,
                I´m a newbie with VBS, and couldn't figure out that ;-)
                 
                This is a wonderful script and with some more features like
                delete from line x to line y  (example line 5-10)
                or delete line x, line y, line z (example line 5,8,10)
                delete line, lines who begins with xxx

                delete line, lines who ends with xxx
                you should have a very powerful Delete Line Script.
                 
                Thanks for great work, time and support.
                 
                Cheers
                /RS
                 
                #8
                  TomRiddle

                  • Total Posts : 620
                  • Scores: 12
                  • Reward points : 0
                  • Joined: 2/7/2008
                  • Location: Australia
                  • Status: offline
                  RE: Delete Line Function Thursday, April 23, 2009 10:46 AM (permalink)
                  0
                  No worries. Most scripts you need to splice bits together out of other scripts or modify one that nearly does what you want.
                  It is rare you will find one that does exactly what you want without the need of modification.
                  If you post what have and what you have tried etc in this particular forum http://www.visualbasicscript.com/forumid_2/tt.htm people will surely help you get it right.
                   
                  #9
                    Mopat

                    • Total Posts : 3
                    • Scores: 0
                    • Reward points : 0
                    • Joined: 7/7/2010
                    • Status: offline
                    Re: RE: Delete Line Function Thursday, July 08, 2010 12:46 AM (permalink)
                    0
                    On this note, I was wondering if one of you can help me.

                    I have a code that displays a line in a host file,
                    Once I run the .vbs file, the new line is added.


                    Now, I need a code to delete that very line that I just added on the same host file.

                    Here is the code I have that displays the new line and im trying to figure a way to delete that line that was added on the host file:

                    strFile = InputBox ("Enter File Name",,"C:\Documents and Settings\t15GOY\Desktop\IDW\hosts")
                    Const ForAppending = 8
                     
                    set objFSO = CreateObject("Scripting.FileSystemObject")
                    set objFile = objFSO.OpenTextFile(strFile, ForAppending, true)
                     


                    objFile.WriteLine("10.08.24.19 bi-reports.newyorklife.com")
                    MsgBox "Mo>Henry"










                     
                    #10
                      TomRiddle

                      • Total Posts : 620
                      • Scores: 12
                      • Reward points : 0
                      • Joined: 2/7/2008
                      • Location: Australia
                      • Status: offline
                      Re: RE: Delete Line Function Thursday, July 08, 2010 11:45 AM (permalink)
                      0
                      Try this function with the following syntax:-

                      DeleteLine
                      "C:\Documents and Settings\t15GOY\Desktop\IDW\hosts", "10.08.24.19 bi-reports.newyorklife.com", 0, 0
                      -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
                       
                      #11

                        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