Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

Replace unknown text in text file by line number

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
glasairguy
Asked:
glasairguy
1 Solution
 
dosthCommented:
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
 
glasairguyAuthor Commented:
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
 
sr75Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now