Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtain Text File Last Character 1

Status
Not open for further replies.

paulcooke

Programmer
Dec 11, 2003
35
0
0
GB
Hello basically have got a text file which we write accounting information to. Unfortunately the customer wishes this file to end with a full stop charatcer. Therefore when we write some new records we are currently reading in all the lines and writing them to a temp file without the full stop character we then append the new lines and then add the full stop in at the end. This can be as you might expect a time consuming process. Is there anyway to open the file simply remove the last character append the new lines and end with the full stop. Any help would be gratefully received thanks
 
My approah to this problem would be to use the FileSystemObject to read in the text file. Use the ReadAll method instead of line by line. Then, strip the last character and add your new stuff. Then, save the file. The performance should be pretty good unless you have huge files to process.

Here's some code to get you started.

Code:
Dim FSO As Scripting.FileSystemObject
Dim strData As String

Set FSO = CreateObject("Scripting.FileSystemObject")

strData = FSO.OpenTextFile("C:\Folder\File.ext", ForReading).ReadAll

strData = Left(stdata, Len(strData) - 1)
strData = strData & vbCrLf & WhateverElse

Call FSO.CreateTextFile("C:\Folder\File.ext", True).Write(strData)

Set FSO = Nothing

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
This is one of those occassions, IMO, where the limitations of the FileSystemObject make themselves felt - specifically the inability to quickly seek to any position in a file without having to actually read it.

In this particular case I think I'd be tempted to revert to the classical file handling methods, and look at something like:
Code:
[blue]    Dim hFile As Long
    hFile = FreeFile
    Open "C:\Folder\File.ext For Binary Access Write As hFile
    Put #hFile, LOF(hFile), strData & "."
    Close hFile[/blue]

 
I had used this one previously in a similar scenario.

Code:
[Red]
Function readFile_ExceptLastChar(fileName As String) As String

Dim fNum As Integer
Dim fileContents As String

    fNum = FreeFile
    
    Open fileName For Binary As #fNum
        fileContents = String(LOF(fNum) - 1, 65)
        Get #fNum, , fileContents
    Close #fNum
    
    readFile_ExceptLastChar = fileContents
    
End Function
[/Red]

Now to apply this to your program, just use it as follows.

Code:
[Red]
MsgBox readFile_ExceptLastChar(fileName) & strNewContents & "."

[/Red]

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top