Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.basic.visual.misc > #1857

Re: Replacing text in a text file with VB6

From "Nobody" <nobody@nobody.com>
Newsgroups comp.lang.basic.visual.misc
Subject Re: Replacing text in a text file with VB6
Date 2011-02-08 15:03 -0500
Organization Aioe.org NNTP Server
Message-ID <iis7k0$en6$1@speranza.aioe.org> (permalink)
References <c45da097-c1e0-4972-bae0-cad00813ca40@s28g2000prb.googlegroups.com>

Show all headers | View raw


"jason@smkzone.com" <jbodine1@yahoo.com> wrote in message 
news:c45da097-c1e0-4972-bae0-cad00813ca40@s28g2000prb.googlegroups.com...
> Hi all,
>
> I'm trying to figure out how to replace text in a text file with a new
> string.  The replace function seems to be the way to go, but I can't
> get it to work right.
>
> The program is a time clock for employees to punch in and out at a
> business.  It uses ADOX for its database and creates a separate text
> file for each employee which the clock in and clock out timestamps are
> written to.  The top 3 lines of each file containe 1. The employee's
> name, 2. His or her SSN, and 3. His or her employee number.
>
> If an employee's name or SSN changes, I have no problem updating them
> in the database.  What I am having a problem with updating them in the
> text files.  I need to be able to replace the existing text with the
> new information which has been stored in the database.

I don't know why the redundancy, it's a recipe for inconsistency. Why not 
regenerating the file each time from the DB? That way you can be sure that 
the information is correct.

Also, VB doesn't have a truncate function, so you have to recreate the 
file(For Output). So if you are replacing one text with a shorter text, you 
end up with extra characters after the text that you have replaced. Many 
rename the existing file, then write the new contents to a new file.

Example code:

Dim sFileContents As String
Dim i As Long
Dim sLines
Dim f As Integer

sFileContents = GetEntireFile("C:\Test.txt")

' Split the file into lines
sLines = Split(sFileContents, vbCrLf)
For i = LBound(sLines) To UBound(sLines)
    Debug.Print sLines(i)
Next

' Modify one line
sLines(2) = "Hello"

' Write the modified file
f = FreeFile
Open "C:\Test.txt" For Output As f
For i = LBound(sLines) To UBound(sLines)
    Print #f, sLines(i)
Next
Close f

' Reads the entire file and returns it as String
Public Function GetEntireFile(ByRef FileName As String) As String
    Dim f As Integer
    Dim sFileContents As String

    f = FreeFile
    Open FileName For Binary Access Read Shared As f
    ' Allocate buffer for the entire file
    sFileContents = String(LOF(f), 0)
    ' Read the entire file into memory
    Get f, , sFileContents
    Close f
    GetEntireFile = sFileContents
End Function


Back to comp.lang.basic.visual.misc | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Replacing text in a text file with VB6 "Nobody" <nobody@nobody.com> - 2011-02-08 15:03 -0500
  Re: Replacing text in a text file with VB6 "Mike Williams" <Mike@WhiskyAndCoke.com> - 2011-02-08 21:08 +0000

csiph-web