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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count the lines of a txt file!!!! How :-)

Status
Not open for further replies.

hpman

Vendor
Dec 12, 2002
6
CA
I need to know how many lines have a txt file
How can i do it

Tkx

;-)
 
While Not EOF
count = count +1
do

This will affect the variable count with many lines the file have! :eek:)
 
And how can i delete only the last line of the file
Can you help me?

Tkx

;-)
 


Public Sub DeleteLastLine()

Dim iInFn As Integer
Dim iOutFn As Integer
Dim lLines As Long
Dim sLine As String
Dim l As Long

iInFn = FreeFile()

Open "YourFileHere" For Input As iInFn

iOutFn = FreeFile()

Open "AnyTemporaryFile" For Output As iOutFn

Do While Not EOF(iInFn)

lLines = lLines + 1

Line Input #iInFn, sLine

Print #iOutFn, sLine

Loop

Close #iOutFn

Close #iInFn


Open "AnyTemporaryFile" For Input As iInFn

Open "YourFileHere" For Output As iOutFn

lLines = lLines - 1

For l = 1 To lLines

Line Input #iInFn, sLine

Print #iOutFn, sLine

Next

Close #iOutFn

Close #iInFn

End Sub

 
Sorry jjames, but the code only pass de lines to the tmp file with returns in the end of the lines

(e.g

sdfsajdfjsdlfkjsldjflksjdfçlsadflskjdflçskadjf
(Return)
asdlfjsalkdfjlskadfjlksjflskjdflksadljkf
(Return)
sfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsad
(Return)
sdfsfsafsadfsadfsadfsadfsadfsadfsdafsdfsdf
)

Can you help?

Tkx

;-)



 
I don't understand what you mean. Each line in a text file is defined as a line because of the line feed (Return). Can you post an actual example of your text file?
 
It's Working now. Tkx for the help

:)
 
The above is a REALLY inefficient way to do this.

The Following SHOULD work for VB6 +. At the moment I'm having some "ISSUES" with references, so it won't execute on my system, but you could try it on a (hopefully small) test file.

Code:
Public Function basLoseLastLine(fName As String, _
                               Optional RecSep As String = vbCrLf, _
                               Optional FldSep As String = ",") As Variant

    Dim Fil As Integer
    Dim RawFile As String                       'Holds the entire contents of the file
    Dim RawSplit() As Variant                    'the file split up on a line per line basis
    Dim RptAry() As String                      '2d array. Holds X lines & 0 to 4 elements per line

    Fil = FreeFile                              'get the next free file number

    Open fName For Binary As #Fil               'Open file
    RawFile = String$(LOF(Fil), 32)             'Create "empty" String of Length
    Get #Fil, 1, RawFile                        'Fill "Empty Str with File

    'Get the Nunber of Records and Fields
    RawSplit = Split(RawFile, RecSep)           'Split the file up by lines

    'Lose the Last Element of the Array
    ReDim Preserve RawSplit(UBound(RawSplit) - 1)

    'Put this Back together
    RawFile = Join(RawSplit, vbCrLf)
    Put #Fil, 1, RawFile                        'Replace Original File

    Close #Fil                                  'Close File
    

End Function

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top