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

x
?
Solved

File Read Question

Posted on 2002-05-22
8
Medium Priority
?
174 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 200 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
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.

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

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.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

926 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