Modify *.txt file in VB6

I have a text file named something like 192.168.1.159.txt  It contains data like this:

1,101598,0,"15:34:43.416",1,1
1,101571,0,"15:34:43.811",1,1
1,101554,0,"15:34:43.609",1,1
2,101554,0,"15:34:58.299",2,2
3,101554,0,"15:34:59.225",2,3
3,101554,0,"15:35:02.467",2,3
3,101554,0,"15:35:06.144",1,3
1,101554,0,"15:35:08.182",1,1
1,101554,0,"15:35:13.465",1,1

I want to remove one line from the file (it could have thousands of records).  My plan is to create a temp.txt file, copy all the records except the one I don't want from the old file to the temp file, kill the old file, and rename the new file.  Here is what I have so far:
    sBadRcd = lstFoundRcd.Text
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    fname = sPath & "rfid\temp.txt"
    Set ts = fs.CreateTextFile(fname)

    iFileNum = FreeFile()
    Open sRsltsFile For Input As iFileNum
    sFileData = Input(LOF(iFileNum), #iFileNum)
    Close iFileNum
    
    LineArr = Split(sFileData, vbCrLf)
    sFileData = vbNullString
    
    'get all records except the one I want to delete
    For i = 0 To UBound(LineArr) - 1
        'if this record is not equal to sBadRcd then copy to temp.txt
    Next i
    
    Kill sRsltsFile
    
    'rename new file
    
    Set fs = Nothing

Open in new window


If some one could help fill in the missing pieces, make corrections as needed, or offer a better suggestion that would be awesome!

Thanks in advance!!!
Bob SchneiderCo-OwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertFabrice LambertCommented:
Hi,

You may find easyer to use the FSO Library (Scripting.FileSystemObject) instead of the build-in file input / output function (wich are a nightmare to use).
Dim FSO As Scripting.FileSystemObject
Dim iStream As Scripting.TextStream
Dim oStream As Scripting.TextStream
Dim tmpPath As String
Dim row As String
Dim rowData() As String
Dim sRsltsFile As String

    '// your source file path here
sRsltsFile = "c:\yadda yadda yadda.txt"

Set FSO = CreateObject("Scripting.FileSystemObject")
    '// get a temp path and file name
tmpPath = FSO.GetSpecialFolder(TemporaryFolder).path & "\" & FSO.GetTempName

    '// open the source file
Set iStream = FSO.OpenTextFile(sRsltsFile, ForReading)
    '// open the temp file
Set oStream = FSO.OpenTextFile(tmpPath, ForWriting, True)
    '// read the source file
While Not (iStream.AtEndOfStream)
    row = iStream.ReadLine
    rowData = Split(row, ",")
    '// ...
    '// add data filtering here
    '// ...
    
    '// if data is OK, write it
    oStream.WriteLine row
Wend

    '// close files
iStream.Close
Set iStream = Nothing
oStream.Close
Set oStream = Nothing

    '// replace the source file by the temp file
FSO.copyFile tmpPath, sRsltsFile, True

    '// delete the temp file
FSO.DeleteFile tempPath

    '// cleanup
Set FSO = Nothing

Open in new window

Note: This is written with early binding. You'd better use late binding.
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
0
Bob SchneiderCo-OwnerAuthor Commented:
Mike, I will check out those examples.  Thank you.

Fabrice, what do you mean early and late binding?
0
Fabrice LambertFabrice LambertCommented:
With early binding, you need to add a reference to the libraries you use.
This is conveniant when developping, as you have access to IntelliSense.
But, early binding is a source of troubles when deploying your application. Your clients must have the exact same Library as the one you use, or newer versions (references upgrade themselves without troubles).

But, if you clients install the application on a computer who has older libraries, references won't downgrade, and your application won't run correctly.
In that case, it is better to use late binding (wich mean references are resolved at run time).
To do this, you declare your variables as Object, and use the CreateObject instruction to instanciate.
You'll also need to provide constant replacement.

Sample code below, using early binding, followed by the same using late binding:
    '// early binding code
Dim fso As Scripting.FileSystemObject
Dim iStream As Scripting.TextStream

Set fso = New Scripting.FileSystemObject
Set iStream = fso.OpenTextFile("c:\myFile.txt", ForReading)

iStream.Close
Set iStream = Nothing
Set fso = Nothing

Open in new window

    '// late binding code
Const ForReading = 1
Dim fso As Object
Dim iStream As Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set iStream = fso.OpenTextFile("c:\myFile.txt", ForReading)

iStream.Close
Set iStream = Nothing
Set fso = Nothing

Open in new window

Conclusion:
Early binding = more convinience for development
Late binding = less troubles for deployment.
0
GrahamSkanRetiredCommented:
Here is the 'nightmare' (non FSO) version
Sub CopyTextFile()

    Dim iInputFileNum As Integer
    Dim iOutputFileNum As Integer
    Dim strFileName As String
    Dim strSaveName As String
    Dim strLine As String
    
    strFileName = "C:\Test\MyTest.txt"
    strSaveName = Replace(strFileName, ".txt", "_old.txt")
    Name strFileName As strSaveName 'rename original file
    
    iInputFileNum = FreeFile()
    Open strSaveName For Input As iInputFileNum 'open original under the new (_old_ name
        iOutputFileNum = FreeFile()
        Open strFileName For Output As iOutputFileNum 'open new file using the original name
            Do Until EOF(iInputFileNum)
                Line Input #iInputFileNum, strLine
                If strLine <> "text of line to be omitted" Then
                    Print #iOutputFileNum, strLine
                End If
            Loop
        Close #iOutputFileNum
    Close #iInputFileNum

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.