Results 1 to 10 of 10

Thread: looping through text file

  1. #1

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

    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

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

    Re: looping through text file

    If it's always in the same format ("N ") then try this:
    VB Code:
    1. Dim ln As String
    2. Dim ff1 As Integer: ff1 = FreeFile
    3. Dim ff2 As Integer: ff2 = FreeFile + 1
    4.     Open "c:\test.txt" For Input As #ff1 'change the filename...
    5.         Open "c:\test_output.txt" For Output As #ff2 '-||-
    6.             Do Until EOF(ff1)
    7.                 Line Input #ff1, ln
    8.                     If Left(Trim(ln), 2) = "N " Then
    9.                         ln = "N" & Right(ln, Len(ln) - 2)
    10.                     End If
    11.                         Print #ff2, ln
    12.             Loop
    13.         Close #ff2
    14.     Close #ff1

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: looping through text file

    this could also be an option:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim FF1 As Integer, FF2 As Integer
    3.  
    4.     FF1 = FreeFile
    5.     Open "C:\input.txt" For Input As #FF1
    6.         FF2 = FreeFile
    7.         Open "C:\output.txt" For Output As #FF2
    8.             Print #FF2, Replace(Input(LOF(FF1), #FF1), vbCrLf & "N ", vbCrLf & "N")
    9.         Close #FF2
    10.     Close #FF1
    11. End Sub
    Last edited by bushmobile; Dec 7th, 2006 at 04:49 PM.

  4. #4

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: looping through text file

    good spot - but i'd prefer not to +1, so i've modified the post in #3

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

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

  7. #7

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

    Re: looping through text file

    Hi

    I'm going to try that.

    Thanks for the help

    Reston

  8. #8
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    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.

  9. #9

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

    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

  10. #10
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: looping through text file

    In VB you use
    VB Code:
    1. Number = (Number + 1) Mod 100000 +1

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