Replace unknown text in text file by line number

Posted on 2008-06-21
Last Modified: 2008-06-23
I know the line number of the text in the text file. I don't know what the text is but I want to replace it with new text. How can I do this?
Question by:glasairguy
LVL 15

Expert Comment

ID: 21836822
try some think like this


Public Function ReplaceLine(fName As String,sText As String, LineNumber As Long) _

     As Boolean

'Purpose: Replace a Line from a text file

'Parameters: fName = FullPath to File

'            LineNumber = LineToDelete

'Returns:    True if Successful, false otherwise

'Requires:   Reference to Microsoft Scripting Runtime

'Example: ReplaceLine("C:\Myfile.txt", "TEST Replace", 3)

'           Replace third line of Myfile.txt



  Dim oFSO As New FileSystemObject

  Dim oFSTR As Scripting.TextStream

  Dim ret As Long

  Dim lCtr As Long

  Dim sTemp As String, sLine As String

  Dim bLineFound As Boolean


  On Error GoTo ErrorHandler

  If oFSO.FileExists(fName) Then

     Set oFSTR = oFSO.OpenTextFile(fName)

    lCtr = 1

     Do While Not oFSTR.AtEndOfStream

        sLine = oFSTR.ReadLine

        If lCtr <> LineNumber Then

            sTemp = sTemp & sLine & vbCrLf


            bLineFound = True

            sTemp = sTemp & sText & vbCrLf

        End If

        lCtr = lCtr + 1




     Set oFSTR = oFSO.CreateTextFile(fName, True)

     oFSTR.Write sTemp


    ReplaceLine = bLineFound

   End If



On Error Resume Next


Set oFSTR = Nothing

Set oFSO = Nothing

End Function


Open in new window


Author Comment

ID: 21838012
My Visual Studio 8, Visual Basic doesn't like the terms "FileSystemObject" & "Scripting.TextStream".
Perhaps I selected the incorrect language I am using when I submitted my question...

LVL 15

Expert Comment

ID: 21839353
Are you writing a program, or just need a script?  You shouldn't need a compiler to run a script and that is what dotsh provided.  You just need to save it as a text document with the .vbs extension instead of .txt.
LVL 85

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 21839757
Here ya go...
        Dim fileName As String = "c:\someFile.txt"

        Dim lineNumber As Integer = 2 ' 1 (one) based line number!

        Dim lineText As String = "this is the line I want to insert"

        Dim lines() As String = Microsoft.VisualBasic.Split(My.Computer.FileSystem.ReadAllText(fileName), vbCrLf)

        If lines.Length >= linenumber Then

            lines(lineNumber - 1) = lineText

        End If

        My.Computer.FileSystem.WriteAllText(fileName, String.Join(vbCrLf, lines), False)

Open in new window


Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now