|
-
Feb 16th, 2006, 12:41 AM
#1
Thread Starter
New Member
Add Row Number to Text File
I'm have a hard time with VB and try to use SQL to do the things I need to do. I'm stuck on this one and need a row number added to a text file. I found this code searching the internet, and think it will work. The problem I have is I can't get it working in VB. I'm getting it needs a subMain error. I'm not sure where (how) the file is named. Should this have a button?
Here is an example of what I'm trying to accomplish.
I have an ascii file that has records of varying length:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccccccccccccccccccccccc
dddddddddddddddddddddd
eeeeeee
fff
ggggggggggggggggggggggggggggggggggggggggggggggggg
I need to add a leading sequence number to these transactions, with padded zeros. So the output will look like this:
000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
000002bbbbbbbbbbbbbbbbbbb
000003ccccccccccccccccccccccccccccccccccccccccc
000004dddddddddddddddddddddd
000005eeeeeee
000006fff
000007ggggggggggggggggggggggggggggggggggggggggggggggggg
-
Feb 16th, 2006, 12:42 AM
#2
Thread Starter
New Member
Re: Add Row Number to Text File
Here is the code that I think will work. I think it is missing something, like a button or something. I get a needs subMain error.
VB Code:
Private Sub AddLineNumbers(ByVal theFile As String)
If File.Exists(theFile) Then
Dim file1Sr As New StreamReader(theFile)
Dim theLineNumber As Integer = 1
Dim aLine As String
Dim tmpFile As String = "./log.tmp"
' determine if tmp file exists
If File.Exists(tmpFile) Then
File.Delete(tmpFile)
End If
'create the tmp file
Dim fStream As FileStream
Try
fStream = File.Create(tmpFile)
Catch ex As Exception
MessageBox.Show(ex.Message, ex.GetType.Name, MessageBoxButtons.OK)
End Try
fStream.Close()
'create the streamwriter for the tmp file
Dim fileSw As StreamWriter
fileSw = File.AppendText(tmpFile)
While file1Sr.Peek() <> -1
aLine = file1Sr.ReadLine
If aLine.Length > 0 Then
If aLine.Chars(0) = "(" Or aLine.Split(":")(0) = "Server" Then
fileSw.WriteLine(theLineNumber.ToString & " - " & aLine)
theLineNumber += 1
Else
fileSw.WriteLine(aLine)
End If
Else
fileSw.WriteLine(aLine)
End If
End While
file1Sr.Close()
fileSw.Close()
Try
File.Copy(tmpFile, theFile, True)
Catch ex As Exception
MessageBox.Show(ex.Message, ex.GetType.Name, MessageBoxButtons.OK)
Exit Sub
End Try
File.Delete(tmpFile)
Else
MessageBox.Show("File '" & theFile & "' Not Found!", "File Error", MessageBoxButtons.OK)
End If
End Sub
Last edited by Hack; Feb 16th, 2006 at 08:00 AM.
Reason: Added [vbcode] [/vbcode] tags and for more clarity.
-
Feb 16th, 2006, 12:46 AM
#3
Re: Add Row Number to Text File
this looks like you dint specify the startup form in your project->properties->startup form. try and let me know....
If an answer to your question has been helpful, then please, Rate it!
Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.
-
Feb 16th, 2006, 01:14 AM
#4
Thread Starter
New Member
Re: Add Row Number to Text File
OK I think I did it right. I did a standard EXE, then dropped this code into the Form1 code area. When I try to make an Project1.exe, I get a compile syntex error.
-
Feb 16th, 2006, 02:33 AM
#5
Re: Add Row Number to Text File
post the code where you get the syntax error...
If an answer to your question has been helpful, then please, Rate it!
Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.
-
Feb 16th, 2006, 02:51 AM
#6
Re: Add Row Number to Text File
Since your code is .NET, post your question in the .NET forum as well.
-
Feb 16th, 2006, 05:45 AM
#7
Thread Starter
New Member
Re: Add Row Number to Text File
LOL, I thought is was VB 6 code.
How much different would this code be in VB6?
Last edited by swtrans; Feb 16th, 2006 at 06:02 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|