looping through text file
Hey everyone
I've been using this code for awhile now and I'm trying to mod it. I'm having no luck thus far.
I want to remove the empty space between the "N" and the number that follows it.
Any help would be nice
%
O0000
(PROGRAM NAME - U-CUT2 )
(DATE=DD-MM-YY - 19-06-06 TIME=HH:MM - 12:40 )
N 100 G20
N 102 G0 G17 G40 G49 G80 G90
N 104 G91 G28 Z0.
N 106 G91 G28 X0. Y0.
N 108 G92 X0. ? Y0. ? Z0. ?
N 110 G0 G90 G52 X2.847 Y0. S2000 M3
N 112 Z.25
N 113 G04 P1
N 114 Z.1
N 115 G04 P1
N 116 G1 Z0. F10.
N 118 X2.848
N 119 G04 P1
N 120 X2.849
N 121 G04 P1
N 122 X2.85
N 123 G04 P1
N 124 X2.851
N 125 G04 P1
VB Code ________________________________
Private Sub Command1_Click()
Dim sContent As String
Dim iInputFile As Integer
Dim iOutputFile As Integer
Dim iFirstSpace As Integer
Dim iLineNumber As Integer
iInputFile = FreeFile()
Open "C:\Ni\Nc\Nc.T" For Input As #iInputFile
iOutputFile = FreeFile()
Open "C:\Ni\Nc\Nc2.T" For Output As #iOutputFile
Do While Not EOF(iInputFile)
Line Input #iInputFile, sContent
Print #iOutputFile, sContent
iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
If Mid$(sContent, iFirstSpace, 1) = " " Then ' Check the first character after the first space
'iLineNumber = CInt(Mid$(sContent, 2, iFirstSpace - 2)) + 1 ' Get the line number, then add 1
Print #iOutputFile, "" '& iLineNumber & "" ' Create and print the new line
End If
Loop
Close #iInputFile
Close #iOutputFile
End Sub
Re: looping through text file
If it's always in the same format ("N ") then try this:
VB Code:
Dim ln As String
Dim ff1 As Integer: ff1 = FreeFile
Dim ff2 As Integer: ff2 = FreeFile + 1
Open "c:\test.txt" For Input As #ff1 'change the filename...
Open "c:\test_output.txt" For Output As #ff2 '-||-
Do Until EOF(ff1)
Line Input #ff1, ln
If Left(Trim(ln), 2) = "N " Then
ln = "N" & Right(ln, Len(ln) - 2)
End If
Print #ff2, ln
Loop
Close #ff2
Close #ff1
Re: looping through text file
this could also be an option:
VB Code:
Private Sub Command1_Click()
Dim FF1 As Integer, FF2 As Integer
FF1 = FreeFile
Open "C:\input.txt" For Input As #FF1
FF2 = FreeFile
Open "C:\output.txt" For Output As #FF2
Print #FF2, Replace(Input(LOF(FF1), #FF1), vbCrLf & "N ", vbCrLf & "N")
Close #FF2
Close #FF1
End Sub
Re: looping through text file
Yeah... it's just that one of FF's should be "FreeFile + 1".
Re: looping through text file
good spot - but i'd prefer not to +1, so i've modified the post in #3
Re: looping through text file
That correction is much safer.. the +1 method would fail if other files have been previously opened (say #1 and #2), and not all of them have been closed (say only #1 was closed, FreeFile would return 1, and 2 is not available).
Re: looping through text file
Hi
I'm going to try that.
Thanks for the help
Reston
Re: looping through text file
Quote:
Originally Posted by tiguy
Hey everyone
I want to remove the empty space between the "N" and the number that follows it.
Any help would be nice
Copy and Paste to Notepad -> Ctrl+H -> Type "N 1" to find "N1" to replace.
Re: looping through text file
hey that work killer.
Can you guys help me with one more?
What if I have something like this below and the "N" and the line number that goes over 100000 lines.
What I'm looking to do is renumber at 99999 and start again at N1
%
O0000
M00
( Menu File: WCT.120 )
( 12-05-2006 19:00:41 )
N 1 G01G90G49G40G80G17
N 2 G91G28Z0
N 3 G91G28X0Y0A0.
N 4 T 12M06
N 5 G92X 119450 Y 151150 Z 229765 A- 80
N 6 G01G90Y 0 F 2000000
N 7 S 2700 M03
N 8 M08
N 9 G90A0.F 2000000
(Y Side 1 A0 XPART)
N 10 G90A-5.0F 2000000
N 11 G90A0.F 2000000
N 12 X 93729 Y 27602 F 2000000
N 13 Z 91660 F 2000000
N 14 Z 80319 F 200000
N 15 X 93729 Y 27276 F 55000
N 16 X 85928 Y 27755 F 55000
N 17 X 78124 Y 28192 F 55000
N 18 X 70318 Y 28588 F 55000
N 19 X 62510 Y 28942 F 55000
N 20 X 54700 Y 29254 F 55000
N 21 X 46888 Y 29525 F 55000
N 22 X 39076 Y 29754 F 55000
N 23 X 31262 Y 29941 F 55000
N 24 X 23447 Y 30087 F 55000
N 25 X 15631 Y 30191 F 55000
N 26 X 7816 Y 30254 F 55000
N 27 X 0 Y 30275 F 55000
N 28 X-7816 Y 30254 F 55000
N 29 X-15631 Y 30191 F 55000
N 30 X-23447 Y 30087 F 55000
N 31 X-31262 Y 29941 F 55000
N 32 X-39076 Y 29754 F 55000
N 33 X-46888 Y 29525 F 55000
N 34 X-54700 Y 29254 F 55000
N 35 X-62510 Y 28942
Re: looping through text file
In VB you use
VB Code:
Number = (Number + 1) Mod 100000 +1