Solved

File Read Question

Posted on 2002-05-22
8
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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
Course of the Month5 days, 12 hours left to enroll

627 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