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

Add line number to text file 1

Status
Not open for further replies.

jcw5107

Technical User
Jan 31, 2007
66
US
I have a text file where I need to add the line number on each line. I have some code that "cleans the text file up" before it is imported into a table. Is there a way to print the line number for each line (on the left or begining of each line) when the text data is written to a new file..??
I'm just havin' troubles doin' this + finding a post that has an example to work from... The text file will have about 150 to 200 lines on it.
Below is the code that cleans the file up and prints it to a new .txt file..
Any suggestions or examples...???
Thanks in advance..!!
jcw5107

Sub MakePRFSWfile()
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim strLine 'As Variant
Dim strType As Variant
Dim strImportFile As String
Dim strNewfile As String
Set db = CurrentDb

strImportFile = "D:\UPSDATA\PRFSW.txt"
strNewfile = "D:\UPSDATA\PRFSW1.txt"

Open "D:\UPSDATA\PRFSW1.txt" For Input As #1
Open strImportFile For Output As #2

Do Until EOF(1)
Line Input #1, strLine
If Left(strLine, 3) Like "N#*" Then
strType = strLine
Print #2, strType
End If
Loop
Close #1
Close #2

DoCmd.TransferText acImportFixed, "PRFSW Spec", _
"PRFSWVarying", strImportFile, False

Kill strNewfile
End Sub
 
Define a variable to hold an integer value. Start it at 1 (or 0 depending on your thought...I am a zero-based array person). Write that value with each line and then increment it as you loop.

Code:
Dim intLineNumber As Integer
intLineNumber = 1
Do Until EOF(1)
Line Input #1, strLine
If Left(strLine, 3) Like "N#*" Then
strType = strLine
Print #2, intLineNumber & " " & strType
End If
intLineNumber = intLineNumber + 1
Loop
Close #1
Close #2

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Code:
Sub MakePRFSWfile()
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim strLine 'As Variant
Dim strType As Variant
Dim strImportFile As String
Dim strNewfile As String
[b][/b]
[COLOR=red]Dim num as Integer[/color ]
Set db = CurrentDb
[COLOR=red]num=0[/color ]
strImportFile = "D:\UPSDATA\PRFSW.txt"
strNewfile = "D:\UPSDATA\PRFSW1.txt"
 
Open "D:\UPSDATA\PRFSW1.txt" For Input As #1
Open strImportFile For Output As #2

Do Until EOF(1)
Line Input #1, strLine
If Left(strLine, 3) Like "N#*" Then
[COLOR=red]num = num + 1[/color ]
strType = strLine
Print #2, [COLOR=red]num & [/color ]strType
End If
Loop
Close #1
Close #2

DoCmd.TransferText acImportFixed, "PRFSW Spec", _
"PRFSWVarying", strImportFile, False

Kill strNewfile
End Sub
 
Pwise,

Awesome..!! Fast response..!!
I messed up though...!! I didn't think of this messing with the formatting of my text file. The text file is fixed width...
Is there a way to put the "line number" at the end of each line...? OR - at a specific location in each line..?? This way the text file can maintain the fix width formatting..
Thanks for the help..
jcw5107
 
Sure...this line is the key:

Print #2, intLineNumber & " " & strType

If you want the line number at the end:

Print #2, strType & " " & intLineNumber

Or, if you want to format the number to show leading zeros so it is always the same length, try:

Print #2, Format(intLineNumber, "000") & " " & strType

That will make all numbers look like 001, 002, 003, etc

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Or from pwise's example:

Print #2, strType & num

or

Print #2, format(num, "000") & strType

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top