Solved

File Read Question

Posted on 2002-05-22
8
156 Views
Last Modified: 2010-05-02
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?
0
Comment
Question by:mtbuford
8 Comments
 
LVL 4

Accepted Solution

by:
Glowman earned 50 total points
ID: 7028043
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 7028124
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
 

Author Comment

by:mtbuford
ID: 7028140
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
 
LVL 18

Expert Comment

by:Crash2100
ID: 7028141
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 22

Expert Comment

by:rspahitz
ID: 7028210
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
 
LVL 18

Expert Comment

by:Crash2100
ID: 7028571
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7818418
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
 

Expert Comment

by:SpideyMod
ID: 7909877
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

10 Experts available now in Live!

Get 1:1 Help Now