Results 1 to 27 of 27

Thread: [RESOLVED] renumber text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Resolved [RESOLVED] renumber text file

    Hi

    Can you guys help me with this

    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.
    Last edited by tiguy; Aug 12th, 2007 at 11:49 PM.

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: renumber text file

    I presume the first number after the N is the line number?

    If so...

    VB Code:
    1. Dim LineInfo() As String
    2. Dim s As String
    3.  
    4. Open "C:\YourFile.txt" For Input As #1
    5. Line Input#1, s
    6. LineInfo = Split(s, " ")
    7.  
    8. If LineInfo(1) > "99999" Then
    9. 'Exit code here
    10. End if
    11.  
    12. Loop
    13.  
    14. Close #1
    N 35 X-62510 Y 28942

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: renumber text file

    Will this start renumbering after line 99999

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: renumber text file

    Quote Originally Posted by lintz
    I presume the first number after the N is the line number?

    If so...

    VB Code:
    1. Dim LineInfo() As String
    2. Dim s As String
    3.  
    4. Open "C:\YourFile.txt" For Input As #1
    5. Line Input#1, s
    6. LineInfo = Split(s, " ")
    7.  
    8. If LineInfo(1) > "99999" Then
    9. 'Exit code here
    10. End if
    11.  
    12. Loop
    13.  
    14. Close #1
    N 35 X-62510 Y 28942
    You forgot a Do While Not EOF(1)

  5. #5
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: renumber text file

    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?

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: renumber text file

    Try this:
    VB Code:
    1. Dim file As String
    2. Dim ff As Integer: ff = FreeFile
    3.     Open "c:\test.txt" For Input As #ff
    4.         file = Input(LOF(ff), ff)
    5.     Close #ff
    6.         If InStr(1, file, vbCrLf & "N 99999") Then
    7.             'do your thing...
    8.         Else
    9.             MsgBox "Not enough data?!"
    10.         End If

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: renumber text file

    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

  8. #8
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: renumber text file

    Quote Originally Posted by DigiRev
    You forgot a Do While Not EOF(1)
    You're right, I posted just as I was leaving work yesterday......a little to keen to leave on a Friday

  9. #9
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: renumber text file

    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

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: renumber text file

    this will do as you request,
    VB Code:
    1. Open "datafile.txt" For Input As 1
    2.     filearr = Split(Input(LOF(1), #1), vbNewLine)
    3.     Close 1
    4. '''ReDim filearr(30000)    ' used for testing
    5. '''For i = 0 To UBound(filearr)
    6. '''    filearr(i) = "N " & i + 1
    7. '''Next
    8. For i = 0 To UBound(filearr)
    9.     linearr = Split(filearr(i), " ")
    10.         linearr(1) = (linearr(1) + 9998) Mod 9999 + 1 'if over 9999 it will make line 10000 = 1 etc
    11.         filearr(i) = Join(linearr, " ")
    12. Next
    13. Open "datafile.txt" For Output As 1  'overwrite original file
    14.     Print #1, Join(filearr, vbNewLine)
    15.     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

  11. #11
    Addicted Member
    Join Date
    May 2006
    Location
    New Romney, Kent, UK
    Posts
    232

    Re: renumber text file

    Quote Originally Posted by tiguy
    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

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: renumber text file

    I'm including the g code file.

    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

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: renumber text file

    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?).

    You should check the UBound there too, eg:
    VB Code:
    1. If UBound(linearr) > 0 Then linearr(1) = ( ...

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: renumber text file

    The error is here

    VB Code:
    1. linearr(1) = (linearr(1) + 9998) Mod 9999 + 1 'if over 9999 it will make line 10000 = 1

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: renumber text file

    Hey This is the text file
    Attached Files Attached Files

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: renumber text file

    Well the problem is as I suspected.. and the solution is as I posted earlier. The complete line would be like this:
    VB Code:
    1. [u]If UBound(linearr) > 0 Then[/u] linearr(1) = (linearr(1) + 9998) Mod 9999 + 1 'if over 9999 it will make line 10000 = 1

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: renumber text file

    now im getting a type mismatch error 13

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: renumber text file

    That would be because you are adding a number to a string (linearr(1) + 9998), and when the error occurs, the string does not represent a number.

    You will need to add an extra check to see that linearr(1) is numeric, which you can do like this: If IsNumeric(linearr(1)) Then

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: renumber text file

    hey Si

    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.

    I'm not sure were to put that code you gave me.

    Reston

  20. #20
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: renumber text file

    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:
    1. If UBound(linearr) > 0 Then   'check there are enough items in the array
    2.   If IsNumeric(linearr(1)) Then   'check that the current string actually contains a number
    3.     linearr(1) = (linearr(1) + 9998) Mod 9999 + 1 'if over 9999 it will make line 10000 = 1
    4.   End If
    5. End If

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: renumber text file

    Hey everyone that helped on this. THANKS!!!!!!!!!!!!!

    This is what worked in the end.

    Can any one explain what is going on with all this. Or what book should i get that would get me going with manipulating text files.

    VB Code:
    1. Private Sub Command3_Click()
    2. Open "C:\test.t" For Input As 1
    3.     filearr = Split(Input(LOF(1), #1), vbNewLine)
    4.     Close 1
    5. '''ReDim filearr(30000)    ' used for testing
    6. '''For i = 0 To UBound(filearr)
    7. '''    filearr(i) = "N " & i + 1
    8. '''Next
    9. For i = 0 To UBound(filearr)
    10.     linearr = Split(filearr(i), " ")
    11.         If UBound(linearr) > 0 Then   'check there are enough items in the array
    12.   If IsNumeric(linearr(1)) Then   'check that the current string actually contains a number
    13.     linearr(1) = (linearr(1) + 9998) Mod 9999 + 1 'if over 9999 it will make line 10000 = 1
    14.   End If
    15. End If
    16.         filearr(i) = Join(linearr, " ")
    17. Next
    18. Open "C:\test2.t" For Output As 1  'overwrite original file
    19.     Print #1, Join(filearr, vbNewLine)
    20.     Close 1
    21. End Sub

  22. #22
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: renumber text file

    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.

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: renumber text file

    Hi all

    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


    Thanks Reston
    Last edited by tiguy; May 23rd, 2007 at 05:03 PM.

  24. #24
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: renumber text file

    At first glance that looks ok.. what is the problem?

  25. #25
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: renumber text file

    Quote Originally Posted by tiguy
    Print #FF2, Replace(Input(LOF(FF1), #FF1), vbCrLf & "N ", vbCrLf & "N")
    Not sure what the vbCrLf is for? If you're just trying to remove the space after "N"

    Code:
    Print #FF2, Replace(Input(LOF(FF1), #FF1), "N ", "N")

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: renumber text file

    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
    Last edited by tiguy; Aug 12th, 2007 at 11:51 PM.

  27. #27
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: renumber text file

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width