I have this text file that needs renumbering. The problem is I only want to start if the lines of code gets to N 99999.Some times we have this G code for our cnc machines that get that high. The controller dosn't like any line over 99999.
Open "file.txt" for Input as #1
open "File1.txt" for Output as #2
Do While not eof(1)
Line Input #1,strIn
intCt=intCt+1
print #2,str(intCt) + strIn
loop
close
or change...
Print #2,"N" + str(intCt) + mid(strIn,6,len(strIn))
Is this what you are asking?
I tried the code you guys helped me with but it looks like it doing some different than what i'm looking for.
I do not want to do anything with any "N" and the line number up to 99999 line of g code. I neeed to loop through each line and evaluate the N1,N2 ect... when it reaches N99999 i need to start renumbering at N1 and go up to N99999 again.
Leave an example of the finish text
%
O0000
M00
( Menu File: WCT.h )
( 12-05-2006 19:00:41 )
N 1 G01G90G49G40G80G17
N 2 G91G28Z0
N 3 G91G28X0Y0
N 4 T 12M06
*** or ***
%
O0000
M00
( Menu File: WCT.h )
( 12-05-2006 19:00:41 )
1 G01G90G49G40G80G17
2 G91G28Z0
3 G91G28X0Y0
4 T 12M06
linearr(1) = (linearr(1) + 9998) Mod 9999 + 1 'if over 9999 it will make line 10000 = 1 etc
filearr(i) = Join(linearr, " ")
Next
Open "datafile.txt" For Output As 1 'overwrite original file
Print #1, Join(filearr, vbNewLine)
Close 1
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
I tried the code you guys helped me with but it looks like it doing some different than what i'm looking for.
I do not want to do anything with any "N" and the line number up to 99999 line of g code. I neeed to loop through each line and evaluate the N1,N2 ect... when it reaches N99999 i need to start renumbering at N1 and go up to N99999 again.
Sorry if this seems complicated.
Reston
Is the code software generated and hand edited, or totally hand written, or a bit of both? I ask because the software (default) usually increments in larger increments.
When you restart numbering again, would it not also, be prudent to (at least) renumber in steps of five, to give the programmer the option of including more lines of code. Ideally you need an option to vary the steps, or even renumber the whole program (don't you think?)
Just a thought.
Last edited by GettinBetter; Dec 9th, 2006 at 07:26 AM.
Reason: typo
I had to delete some of it to fit on this web site.
I tried this below and i get an error. ( Subscipt out of range )
I changed the file to write to to keep the old file for comparision.
Any ideas
Thanks Reston
vb code ______________________________________________
Private Sub Command3_Click()
Open "C:\test.t" For Input As 1
filearr = Split(Input(LOF(1), #1), vbNewLine)
Close 1
'''ReDim filearr(30000) ' used for testing
'''For i = 0 To UBound(filearr)
''' filearr(i) = "N " & i + 1
'''Next
For i = 0 To UBound(filearr)
linearr = Split(filearr(i), " ")
linearr(1) = (linearr(1) + 9998) Mod 9999 + 1 'if over 9999 it will make line 10000 = 1 etc
filearr(i) = Join(linearr, " ")
Next
Open "C:\test2.t" For Output As 1 'overwrite original file
Print #1, Join(filearr, vbNewLine)
Close 1
End Sub
First of all, when you post code please put it inside VBCode tags - either using the button in the post editor screen, or by putting them in manually, like this: [vbcode] 'code here [/vbcode]
Next, where is the error?
I presume it would be on the "linearr(1) = " line, as that is the only one that references an array with an index which may not be valid. All uses of i are fine, as you use UBound to keep it in range, however on that line you specify item 1 when there may not be any spaces in the line (perhaps the last line of the file is blank?).
Thanks for the help but this seem to be a little over my head. I think i thought this would be an easy thing. Arrays are something that i havn't work with before.
The fact that it is an array is irrelevant for this part - the important thing here is that it contains a string, and you have to ensure that it can be converted to a number before trying to use it as one.
What you need is something like this:
VB Code:
If UBound(linearr) > 0 Then 'check there are enough items in the array
If IsNumeric(linearr(1)) Then 'check that the current string actually contains a number
linearr(1) = (linearr(1) + 9998) Mod 9999 + 1 'if over 9999 it will make line 10000 = 1
Everything you need to manipulate text files is the "Open"/"Input"/"Print" and "Close".
The rest of the code is essentially manipulating strings (they came from the file, but that is irrelevant), which for the sake of simplicity was done using arrays (even tho you don't know them yet, the code is shorter & faster than the alternative).
To learn about arrays I'd recommend reading the article we have in our Classic VB FAQ's... it should answer most of your questions. Any more you have, feel free to post them here.
I,ve been using this code for a while. i wanted to run through again and remove any empty spaces.
Can anyone help with this
Reston
VB Code *******************
Private Sub Command3_Click()
Open App.Path & "\test.t" For Input As 1
filearr = Split(Input(LOF(1), #1), vbNewLine)
Close 1
'''ReDim filearr(30000) ' used for testing
'''For i = 0 To UBound(filearr)
''' filearr(i) = "N " & i + 1
'''Next
For i = 0 To UBound(filearr)
linearr = Split(filearr(i), " ")
If UBound(linearr) > 0 Then 'check there are enough items in the array
If IsNumeric(linearr(1)) Then 'check that the current string actually contains a number
linearr(1) = (linearr(1) + 99998) Mod 99999 + 1 'if over 9999 it will make line 10000 = 1
ace = 89
End If
End If
filearr(i) = Join(linearr, " ")
Next
Open App.Path & "\test.t" For Output As 1 'overwrite original file
Print #1, Join(filearr, vbNewLine)
Close 1
Command1.Enabled = True
End Sub
Private Sub Command1_Click()
Then i run this to remove the space after the N. I would like to have a Separate command button that i can run the remove all spaces.
Dim FF1 As Integer, FF2 As Integer
FF1 = FreeFile
Open App.Path & "\test.t" For Input As #FF1
FF2 = FreeFile
Open App.Path & "\test2.t" For Output As #FF2
Print #FF2, Replace(Input(LOF(FF1), #FF1), vbCrLf & "N ", vbCrLf & "N")
Close #FF2
Close #FF1
End Sub
What I'm looking for is to remove all the empty spaces from between all the text. This is a text file that I'm working with.
%
O0000
M00
( Menu File: WCT.h )
( 12-05-2006 19:00:41 )
N 1 G01G90G49G40G80G17
N 2 G91G28Z0
N 3 G91G28X0Y0
N 4 T 12M06
N 5 G92X 119450 Y 151150 Z 229765
N 6 G01G90Y 0 F 2000000
N 7 S 2700 M03
Put a checkbox on the form. This code will remove all spaces (while writing out the new file) if the box is checked
Code:
If Check1.Value = vbChecked Then
Print #1, Replace(Join(filearr, vbNewLine), " ", "")
Else
Print #1, Join(filearr, vbNewLine)
End If
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read. Please Help Us To Save Ana