Solved

Replace unknown text in text file by line number

Posted on 2008-06-21
4
532 Views
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?
0
Comment
Question by:glasairguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Expert Comment

by:dosth
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
        Else
            bLineFound = True
            sTemp = sTemp & sText & vbCrLf
        End If
        lCtr = lCtr + 1
    Loop
   
     oFSTR.Close
     Set oFSTR = oFSO.CreateTextFile(fName, True)
     oFSTR.Write sTemp
  
    ReplaceLine = bLineFound
   End If
   
 
ErrorHandler:
On Error Resume Next
oFSTR.Close
Set oFSTR = Nothing
Set oFSO = Nothing
 
End Function
%>

Open in new window

0
 

Author Comment

by:glasairguy
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...

Andy
0
 
LVL 15

Expert Comment

by:sr75
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.
0
 
LVL 86

Accepted Solution

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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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