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!

read file

Status
Not open for further replies.

Footbal76

MIS
Sep 6, 2006
21
US
I'm trying to read this file and I want to find the total records within the file.

Search the file and find the value (ID:) and give the total records found in the file
so I'm trying to loop through.

How can I find the total records in the file based on what I have?

For filecntr = 1 To 3
Filex = "C:\VB PROJECTS\File\KM0" & CStr(filecntr) & "J501"
If (FileExists(Filex) = True) Then
Open Filex For Input As #1
If Not EOF(1) Then
Line Input #1, TextLine
End If
Do While Not EOF(1)
If InStr(TextLine, "ID:") > 0 Then
 
Here is a function that I have used in the past to read a file into an arraylist of lines. You can run this and then use the ArrayList.Count property. You can then work through the lines at your leisure without keeping the file 'in use'.
Code:
Public Shared Function GetLinesFromFile(ByVal FilePath As String, Optional ByVal Lines As Integer = 0) As ArrayList

			Dim strLine As String
			Dim File As StreamReader
			Dim strLines As New ArrayList

			Try
				File = New StreamReader(FilePath)
			Catch ex As Exception			 'e.g. file is open, or doesn't exist
				Throw ex
				Exit Function
			End Try
			Dim i As Integer = 1
			Do
				strLine = File.ReadLine()
				strLines.Add(strLine)
				i += 1
			Loop Until (strLine Is Nothing) Or (i > Lines And Lines > 0)
			Return strLines
		End Function
The Lines argument allows you to limit the number of lines read from the file, probably not useful in your scenario, but I left it in just in case.
 
Code is not looping through file to get each textline to find value: "ID"

Any help please.

Public Sub Email()
Dim File As String
Dim TextLine As String
Dim i As Integer
Dim stucntr As Integer
Dim filecntr As Integer

stucntr = 0
For filecntr = 1 To 3
File = "\C:\VB_PROJECTS\File\JQ0" & CStr(filecntr) & "H301"
If (FileExists(File) = True) Then
Open File For Input As #1
If Not EOF(1) Then
Line Input #1, TextLine HERE ITS NOT GETTING THE LINE. DOES ANYONE SEE WHATS WRONG.
End If
Do While Not EOF(1)
If InStr(TextLine, "ID:") > 0 Then
MsgBox "hello"
End If

Loop
End If
Next


This code checks to see if the file exist
This works fine

Function FileExists(Path As String) As Integer
Dim X As Integer
X = FreeFile

On Error Resume Next
Open Path For Input As X
If Err = 0 Then
FileExists = True
Else
FileExists = False
End If
Close X
End Function
 
I use the following which is from help.

Code:
Imports System
Imports System.IO

Class Test
    Public Shared Sub Main()
        Try
            ' Create an instance of StreamReader to read from a file.
            Using sr As StreamReader = New StreamReader("TestFile.txt")
                Dim line As String
                ' Read and display the lines from the file until the end 
                ' of the file is reached.
                Do
                    line = sr.ReadLine()
                    Console.WriteLine(Line)
                Loop Until line Is Nothing
                sr.Close()
            End Using
        Catch E As Exception
            ' Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(E.Message)
        End Try
    End Sub
End Class

I've changed things here and there (output to text boxes instead, etc) as my handling preferences have changed, but it was a good starting point for me. I got away from Open file... when I came to .net. I can't say it is better or worse, but it seems to be how .net uses files. Of course to write it is StreamWriter.

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top