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

I have A text file that I have To s

Status
Not open for further replies.

rembrechts

Programmer
Apr 10, 2002
11
BE
I have A text file that I have To send to machine that processes the commands in that file. Every command is a line in that text file.
I want to display the command progress. Is there any way I can find out how many lines there are in that file.

The lines have not the same length.

 
In your text file, if Each line has Carriage Return and/or line feed, then you can open the file in VB and using read and instr$ function count no. of carriage Return or line feed in the file, this should give you the total no. of records in the file. you will have to read this file in hex mode to trap CR and/or LF characters.
 
Assuming each coimmandline is on its own line, you can also just read each line entirely with the Line Input# and count the number of lines read:

Dim lngLinesCount As Long
Dim strLine As String
Dim strInputFile As String
lngLinesCount = 0
strInputFile = "commandfile.txt"
Open strInputFile For Input As #1
While Not EOF(1)
lngLinesCount = lngLinesCount + 1
Line Input #1, strLine
If strLine = "" Then
' ignore blank lines
lngLinesCount = lngLinesCount - 1
End If
Wend
MsgBox "File has " & lngLinesCount & " command lines."


Mark
 
You could try this code but it means reading the file in, so may not be appropriate.

---
Dim MyFileName As String
Dim MyFileNum As Integer
Dim linecount As Integer
Dim Dummy As String

MyFileNum = FreeFile
MyFileName = "C:\tmp\wow.txt" ' Insert your filename here
Open MyFileName For Input As #MyFileNum
Do While Not EOF(1)
Line Input #MyFileNum, Dummy
linecount = linecount + 1 ' increment the line count
Loop
Close #MyFileNum
MsgBox "There are " & linecount & " lines in the file " & MyFileName
----

 
DOS has a find command if you could do a find on how many CR/LF there are in a file you might be on to something. You could redirect the output to a file and then read the file back in vb. A bit low tech, but it could work...

FIND [/V] [/C] [/N] [/I] "string" [[drive:][path]filename[ ...]]

/V Displays all lines NOT containing the specified string.
/C Displays only the count of lines containing the string.
/N Displays line numbers with the displayed lines.
/I Ignores the case of characters when searching for the string.
"string" Specifies the text string to find.
[drive:][path]filename
Specifies a file or files to search.

If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top