Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

Replace a line in a file - Read/Write access posible random How do I do this

im looking to replace a line in a file i wrote a funtion to read the config file im useing and look for a string in it then I want to replace that line it finds with the new string how do I do this im relly stuck.. I keep geeting errors like bad mode and file number/name etc...im relly stuck

here is the code I have so far with error checking
and comments notes for me

I also as you can see tryed in the bottom to do it by stream either way is fine with me I just want it to work

Public Function Write_Config(Line_of_Text As String, Look_For As String)
   Dim FileNumber
   Dim strFilNam As String
   Dim TextLine As String
   On Error GoTo ErrorHandler   ' Enable error-handling routine.

   FileNumber = FreeFile   ' Get unused file
   strFilNam = App.Path & "\ircd.conf"

   Do
      Open strFilNam For Random As #FileNumber
      'On Error GoTo 0   ' Turn off error trapping.
      'On Error Resume Next   ' Defer error trapping.

      Line Input #FileNumber, TextLine   ' Read line into variable.
      Debug.Print TextLine   ' Print to the Immediate window.

      If IsInString(TextLine, Look_For) Then
         Print #FileNumber, Line_of_Text
      End If
   Loop Until EOF(FileNumber)   ' Loop until end of file.

   Close
   Exit Function      ' Exit to avoid handler.
ErrorHandler:              ' Error-handling routine.
   Select Case Err.Number   ' Evaluate error number.
      Case 52 'Bad file name or number
         msg = "Error # " & Str(Err.Number) & " was generated by " _
            & Err.Source & Chr(13) _
            & Err.Description & Chr(13) _
            & "File: " & Chr(13) _
            & strFilNam & Chr(13) _
            & "File Number is " & FileNumber

         MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext
         Close
         End
      Case 55   ' "File already open" error.
         Close   ' Close open file.
      Case Else
         Debug.Print Error(Err.Number)   ' Print error to Immediate window.

         msg = "Error # " & Str(Err.Number) & " was generated by " _
            & Err.Source & Chr(13) & Err.Description
         MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext

         Close
         End
   End Select
   Resume   ' Resume execution at same line
   ' that caused the error.

   ' #HOut# ********************
   ' #HOut# Programmer Name  : Pern
   ' #HOut# Date             : 3/18/2003
   ' #HOut# Time             : 18:59
   ' #HOut# Comment          :
   ' #HOut# Comment          :
   ' #HOut# Comment          :
   ' #HOut# ********************
   ' #Out#    Dim a, FS As New FileSystemObject, File As String
   ' #Out#    Close
   ' #Out#    File = App.Path & "\ircd.conf"
   ' #Out#    Set a = FS.OpenTextFile(File, ForAppending, False, TristateFalse)
   ' #Out#       Do
   ' #Out#          frmMain.txtPern.Text = a.ReadLine
   ' #Out#          If IsInString(a.ReadLine, Look_For) Then
   ' #Out#             a.WriteLine Line_of_Text
   ' #Out#          Else: a.SkipLine
   ' #Out#          End If
   ' #Out#       Loop Until a.AtEndOfStream <> True
   ' #Out#    a.Close
   ' #HOut# ********************
End Function

thanks in advance to any help provided
ASKER CERTIFIED SOLUTION
Avatar of QJohnson
QJohnson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Johnny

ASKER

I was able to find an example on exchange (as i was browsing the answers) and moded it to fit my needs...

I thank you for your help in this matter..and the nice explination...
i will award you your points for it..as you did provide a solution and helped...

i have alot of progrmming questions so please look for more from me my next ones about treeview and nodes..

ill post the link here at the bottom for it.

heres what i did to solve the above problem with te search and replace lines... made it a function so i can replace anything i want in the config file..

Public Function Write_Config(Line_of_Text As String, Look_For As String)
   On Error GoTo UpdateErr
   Dim strFile As String
   Dim strTmpFile As String
   Dim strLine As String
   
   strFile = App.Path & "\ircd.conf"
   strTmpFile = App.Path & "\temp_ircd.conf"

   If (Dir(strTmpFile) <> "") Then ' tmp file exist
       Kill strTmpFile ' delete the file
   End If
   
   Open strFile For Input As #1
   Open strTmpFile For Output As #2
   
   ' copy until the line you want
   Do While (Not EOF(1))
       Input #1, strLine
       If IsInString(strLine, Look_For) Then
           Exit Do
       End If
       Print #2, strLine
   Loop
   
   ' write the changed line
   Print #2, Line_of_Text
   
   ' now write the rest of the file
   Do While (Not EOF(1))
       Input #1, strLine
       Print #2, strLine
   Loop
   
   ' close open files
   Close #2
   Close #1
   
   If (Dir(strTmpFile) <> "") Then ' tmp file exist
       Kill strFile ' delete the original file
       ' rename the tmp file as original file
       Name strTmpFile As strFile
   End If

   Exit Function

UpdateErr:
   MsgBox "Error updating the file." & vbCrLf & Err.Number & ": " & Err.Description
   Reset ' close any open files
   On Error GoTo 0
End Function

--------------------------------------------------
Again thanks for your help QJohnson

heres the link for the next problem i have. with treeview and nodes.

https://www.experts-exchange.com/questions/20556122/TreeView-and-Nodes-in-a-read-from-data-to-display.html

Pern
Avatar of Johnny

ASKER

Thanks again..i didnt grade the answer as A: Excellent!!!  as you did not completely give full code..altho i do belive alowing one to figure out th answer in some cases..i really want full code for answers.

you had no way of knowing this and i did not define this befor hand so i didnt deduct anything from awarding...

thanks alot fo the prompt and great answer..

Johnny aka Pern