File Read Question

I have a form with 2 textboxes that on is the input and one output of what I want.  Then I would like to open a file up and read line by line for the input value and if that value is found replace the output value.  Can someone help me with this quick program?
mtbufordAsked:
Who is Participating?
 
GlowmanCommented:
Sure I can give this one a shot.

Public sub ReplaceMyString(byval WhichFile as String, byval NewFile as String, byval strOriginal as String, byval strReplace as String)

Dim FileTool as New Scripting.FileSystemObject
Dim File as Scripting.TextStream
dim NewFile as Scripting.TextStream
Dim Result as String


Set File = FileTool.OpenTextFile( WhichFile,ForAppending,False,TristateUseDefault)

Set NewFile = FileTool.CreateTextFile(NewFile,True,False)


Do Until File.EOF

  Result=File.ReadLine

  If Result = strOriginal Then

    NewFile.WriteLine strReplace

  Else

    NewFile.WriteLine Result

  End If

  DoEvents

Loop

End Sub

This will need a reference to "Microsoft Scripting Runtime"  And it also assumes that you are looking for the entire line to replace not just part of it.  You must pass the entire path to both Files in the sub.  And then pass the Text property of your textboxes into it as well.  Hope this helps

G
0
 
rspahitzCommented:
Without any additional components:

Private Sub ReplaceFileLine(FileName as string, SearchLine as string, ReplaceLine as string)
  on error goto ReplaceFileLine_Err

  dim iFileNumberIn as integer
  dim iFileNumberOut as integer
  dim strFileLine as string

  const cTempFileName as string = "Temp.xxx"
  cnost cSaveFileName as string = "OriginalFile.xxx"

  iFileNumberIn = freefile()
  open FileName for input as #iFileNumberIn

  iFileNumberOut = freefile()
  open cTempFileName for output as #iFileNumberOut

  while not eof(#iFileNumberIn)
    line input #iFileNumberIn, strFileLine
    if strFileLine = SearchLine then
       print #iFileNumberOut, ReplaceLine
    else
       print #iFileNumberOut, strFileLine
    endif
  wend

  close #iFileNumberOut
  close #iFileNumberIn

' Rename temp file to original
  if dir$(cSaveFileName)<> vbnullstring then
     kill cSaveFileName
  endif
  name FileName as cSaveFileName
  name cTempFileName as FileName

  msgbox "Line replacement completed", vbOKOnly or VBInformation
  exit sub

ReplaceFileLine_Err:
  msgbox "Error: " & err.number & " - " & err.description
end sub


To use:
From, for example, a command button, call something like this:

ReplaceFileLine "C:\My documents\xxx.txt", Text1.Text, Text2.Text
0
 
mtbufordAuthor Commented:
Could anyone put this in simple form and send it to me at mbuford@tampabay.rr.com  I would also like it to have a drive and directory boxes to select the file.  
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Crash2100Commented:
Private Sub Command1_Click()
    If Dir(App.Path & "\temp.txt") = "temp.txt" Then Kill (App.Path & "\temp.txt")

    Open App.Path & "\textfile.txt" For Input As #1
    Open App.Path & "\temp.txt" For Output As #2
   
    Dim strLine As String
   
    Do While Not EOF(1)
        Line Input #1, strLine
        If strLine = txtInput.Text Then
            Print #2, txtOutput.Text
        Else
            Print #2, strLine
        End If
    Loop
   
    Close #1
    Close #2
   
    If Dir(App.Path & "\textfile.txt") = "textfile.txt" Then Kill (App.Path & "\textfile.txt")
   
    Name App.Path & "\temp.txt" As App.Path & "\textfile.txt"
End Sub
0
 
rspahitzCommented:
To easily select the file, you WILL need a component:  Windows Common Dialog Control.

After you place it on your form, add this code:

CommonDialog1.ShowOpen

and get the filename from:

CommonDialog1.Filename
0
 
Crash2100Commented:
Private Sub cmdSelect_Click()
    CommonDialog1.ShowSave
    txtFileName.Text = CommonDialog1.FileName
End Sub

Private Sub cmdChangeFile_Click()
    If Dir(App.Path & "\temp.txt") = "temp.txt" Then Kill (App.Path & "\temp.txt")
   
    Open txtFileName.Text For Input As #1
    Open App.Path & "\temp.txt" For Output As #2
   
    Dim strLine As String
   
    Do While Not EOF(1)
        Line Input #1, strLine
        If strLine = txtInput.Text Then
            Print #2, txtOutput.Text
        Else
            Print #2, strLine
        End If
    Loop
   
    Close #1
    Close #2
   
    If Dir(txtFileName) <> "" Then Kill (txtFileName)
   
    Name App.Path & "\temp.txt" As txtFileName
End Sub
0
 
DanRollinsCommented:
Hi mtbuford,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Glowman's comment(s) as an answer.

mtbuford, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.