October 17, 2008 at 4:56 am
Hi All,
i am trying to find out the number of records in a flat file using Script task . I use the following code to read the flat file , find the record which begins with "EOF" , and read that particular line. can any one suggest me how to modify the below code to get the total lines in that flat file?
My code :
*********************************************
fileloc = "C:/data/data1.txt"
Dim oStream1 As New IO.StreamReader(fileloc)
v_eof_Count = 0
While oStream1.Peek <> -1 And v_eof_Count <= 3
strReadLine = oStream1.ReadLine()
If Left(strReadLine, 3) = "EOF" Then
Rcd_Cnt = CInt(Strings.Mid(strReadLine, 106, 10))
filedate = New Date( _
CInt(Strings.Mid(strReadLine, 53, 4)), _
CInt(Strings.Mid(strReadLine, 47, 2)), _
CInt(Strings.Mid(strReadLine, 50, 2)))
eof_flag = "Y"
wait_flag = "N"
Exit While 'leave loop here if EOF found
End If
v_eof_Count = v_eof_Count + 1
System.Threading.Thread.Sleep(5000)
End While
*********************************************
Thanks,
Arunvijay
October 17, 2008 at 5:24 am
here's how i would do it:
first id read everything into a single string, instead of line by line.
then i could use substring functions to find what we need instead.
Dim fileloc As String = "C:/data/data1.txt"
Dim oStream1 As New IO.StreamReader(fileloc)
Dim v_eof_Count = 0
Dim i As Integer
Dim TotalLength As Integer
Dim FileContents As String
Dim CurrLine As String
'read the whole thing
FileContents = oStream1.ReadToEnd
find where the special "EOF" file exists.
might need to find vbCrLf & "EOF" if EOF could exist in the middle of a line of text
i = FileContents.IndexOf("EOF") ' i = FileContents.IndexOf(vbCrLf & "EOF") +2
CurrLine = filecontents.Substring(i,FileContents.IndexOf(vbCrLf,i+1)
'your previous variables
Rcd_Cnt = CInt(Strings.Mid(CurrLine, 106, 10))
filedate = New Date( _
CInt(Strings.Mid(CurrLine, 53, 4)), _
CInt(Strings.Mid(CurrLine, 47, 2)), _
CInt(Strings.Mid(CurrLine, 50, 2)))
'now the counts
TotalLength = FileContents.Length
FileContents = FileContents.Replace(vbCrLf, "")
'the number of lines = the number of VbCRLf's we removed, plus maybe one line?was the real end of file marker at teh end of the last line, or is there a final CrLF at the end?
TotalLength = (TotalLength - FileContents.Length) / 2
Lowell
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply