Results 1 to 22 of 22

Thread: Text file loop

  1. #1

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

    Text file loop

    Hi everyone

    Hey i could use some help with a text file problem. I want to open a very large text (3000lines) file and write "G04 P1" after every "Z" line that is already in the file. Any help?

    This is the start of the cnc gcode program

    %
    O0000
    (PROGRAM NAME - EAD )
    (DATE=DD-MM-YY - 16-06-06 TIME=HH:MM - 10:44 )
    N100 G20
    N102 G0 G17 G40 G49 G80 G90
    N104 G91 G28 Z0.
    N106 G91 G28 X0. Y0.
    N108 G92 X0. ? Y0. ? Z0. ?
    N110 G0 G90 G52 X2.847 Y0. S764 M3
    N112 Z.25
    N114 Z.1
    N116 G1 Z0. F6.1
    N118 X2.852 F12.2
    N120 X2.857
    N122 Z-.0002 #### Start below this line ######
    N124 X2.862
    N126 Z-.0004
    N128 X2.867
    N130 Z-.0007
    N132 X2.872
    N134 Z-.0011
    N136 X2.8769
    N138 Z-.0016
    N140 X2.8819
    N142 Z-.0022
    N144 X2.8869


    Thanks Reston

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

    Re: Text file loop

    You can't edit a text file in place. The best you can do is open the file for read, open another file for write, read each line into a string variable, test if it's a Z line, add "G04 P1" to the end if it is, then write the string variable to the write file regardless.
    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

  3. #3

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

    Re: Text file loop

    I have been writing to a log file for awhile, but have never opened a file and wrote to another like this. Any starter type code would be a great help.

    Reston

  4. #4

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

    Re: Text file loop

    Can anyone help with this?

  5. #5
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Text file loop

    i hope this code will help you
    VB Code:
    1. Dim sContent As String
    2.     Dim iInputFile As Integer
    3.     Dim iOutputFile As Integer
    4.  
    5.     iInputFile = FreeFile()
    6.     Open "E:\MC\sometext.txt" For Input As #iInputFile
    7.     iOutputFile = FreeFile()
    8.     Open "E:\MC\anotherFile.txt" For Output As #iOutputFile
    9.     Do While Not EOF(iInputFile)
    10.         Line Input #iInputFile, sContent
    11.         'If InStr(sContent, "Z") > 0 Then ' If you just want to check for Z
    12.         If UCase(Mid(sContent, 6, 1)) = "Z" Then ' If you just want to check for Z in a specific pos
    13.             Print #iOutputFile, sContent
    14.             Print #iOutputFile, "G04 P1"
    15.         Else
    16.             Print #iOutputFile, sContent
    17.         End If
    18.     Loop
    19.     Close #iInputFile
    20.     Close #iOutputFile
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  6. #6

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

    Re: Text file loop

    Hey that works killer. Thank you soo much.

    I did have one more question about this. I there a way to number the new line that we are putting in. If you look at the G Code file you will see that the file is numbered,But does skip a number. The new line needs to be the next number in sequence.

    Another thing that i just noticed is the if the file numbers reaches 1000 it stops adding the "G04 P1"

    N988 X2.8106
    N990 Z-.7197
    G04 P1
    N992 X2.8142
    N994 Z-.7232
    G04 P1
    N996 X2.8178
    N998 Z-.7267
    G04 P1
    N1000 X2.8213
    N1002 Z-.7302
    N1004 X2.8249
    N1006 Z-.7337
    N1008 X2.8285
    N1010 Z-.7371
    N1012 X2.8322
    N1014 Z-.7406
    N1016 X2.8358
    N1018 Z-.7441
    N1020 X2.8394
    N1022 Z-.7475
    N1024 X2.843
    N1026 Z-.7509
    I think If UCase(Mid(sContent, 6, 1)) may be looking at the sixth charactor.

    Thanks again

    Reston

  7. #7

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

    Re: Text file loop

    Hi all

    I think i figured out part of it.

    Vb Code ______________________________________

    Private Sub Command1_Click()
    Dim sContent As String
    Dim iInputFile As Integer
    Dim iOutputFile As Integer

    iInputFile = FreeFile()
    Open "C:\Documents and Settings\n\Desktop\EAD.t" For Input As #iInputFile
    iOutputFile = FreeFile()
    Open "C:\Documents and Settings\n\Desktop\EAD2.t" For Output As #iOutputFile
    Do While Not EOF(iInputFile)
    Line Input #iInputFile, sContent
    'If InStr(sContent, "Z") > 0 Then ' If you just want to check for Z
    If UCase(Mid(sContent, 6, 2)) = "Z-" Or UCase(Mid(sContent, 7, 2)) = "Z-" Then ' If you just want to check for Z in a specific pos
    Print #iOutputFile, sContent
    Print #iOutputFile, "G04 P1"
    Else
    Print #iOutputFile, sContent
    End If
    Loop
    Close #iInputFile
    Close #iOutputFile


    End Sub

  8. #8
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Text file loop

    Try this:
    VB Code:
    1. Dim sContent As String
    2.     Dim iInputFile As Integer
    3.     Dim iOutputFile As Integer
    4.     Dim iFirstSpace As Integer
    5.     Dim iLineNumber As Integer
    6.    
    7.     iInputFile = FreeFile()
    8.     Open "E:\MC\sometext.txt" For Input As #iInputFile
    9.     iOutputFile = FreeFile()
    10.     Open "E:\MC\anotherFile.txt" For Output As #iOutputFile
    11.     Do While Not EOF(iInputFile)
    12.         Line Input #iInputFile, sContent
    13.         Print #iOutputFile, sContent
    14.         iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
    15.         If Mid$(sContent, iFirstSpace + 1, 1) = "Z" Then ' Check the first character after the first space
    16.             iLineNumber = CInt(Mid$(sContent, 2, iFirstSpace - 2)) + 1 ' Get the line number, then add 1
    17.             Print #iOutputFile, "N" & iLineNumber & " G04 P1" ' Create and print the new line
    18.         End If
    19.     Loop
    20.     Close #iInputFile
    21.     Close #iOutputFile

  9. #9
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Text file loop

    Why not just open the file, get the text, close it, work on that text, then replace the file with that updated text ..??

  10. #10
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Text file loop

    VB Code:
    1. Private Sub Command1_Click()
    2.    
    3.     Dim sContent As String
    4.     Dim sText As String
    5.     Dim sArray() As String
    6.     Dim iFile As Integer
    7.     Dim sTemp As String
    8.     Dim iTemp As Integer
    9.     Dim i As Integer
    10.    
    11.     iFile = FreeFile
    12.     Open "C:\Documents and Settings\n\Desktop\EAD.t" For Input As #iFile
    13.         sContent = Trim$(Input$(LOF(iFile), #iFile))
    14.     Close iFile
    15.     If Len(sContent) Then
    16.         sArray = Split(sContent, vbCrLf)
    17.         For i = 0 To UBound(sArray)
    18.             sText = sText & sArray(i) & vbCrLf
    19.             iTemp = InStr(sArray(i), " ")
    20.             sTemp = Mid$(sArray(i), iTemp + 1, 1)
    21.             If UCase$(sTemp) = "Z" And iTemp > 2 Then
    22.                 sTemp = Mid$(sArray(i), 2, iTemp - 2)
    23.                 If IsNumeric(sTemp) Then
    24.                     sText = sText & "N" & CInt(sTemp) + 1 & " G04 P1" & vbCrLf
    25.                 End If
    26.             End If
    27.         Next i
    28.         Open "C:\Documents and Settings\n\Desktop\EAD.t" For Output As #iFile
    29.             Print #iFile, sText;
    30.         Close iFile
    31.         Debug.Print "Finished"
    32.     End If
    33. End Sub
    Last edited by rory; Jun 18th, 2006 at 06:06 AM.

  11. #11

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

    Re: Text file loop

    Hey Logophobic

    Thanks for all the help on this by the way.

    It looks like my work mates are making this harder on me. the file that i got from them was not the same as what they wanted.

    what you helped me with is awesome but if i may ask you to help with one last thing that would be great.

    This is the problem.

    I can use what you helped me with to add the line number and after X a G04 P1 but i need to add The line number and the G04 P1 after Z line that is on it's own line also.
    %
    O0000
    (PROGRAM NAME - U-CUT2 )
    (DATE=DD-MM-YY - 19-06-06 TIME=HH:MM - 12:40 )
    N100 G20
    N102 G0 G17 G40 G49 G80 G90
    N104 G91 G28 Z0.
    N106 G91 G28 X0. Y0.
    N108 G92 X0. ? Y0. ? Z0. ?
    N110 G0 G90 G52 X2.847 Y0. S2000 M3
    N112 Z.25
    N114 Z.1
    N116 G1 Z0. F10.
    N118 X2.848
    N120 X2.849
    N122 X2.85
    N124 X2.851
    N126 X2.852
    N128 X2.853 Z-.0001
    N130 X2.854
    N132 X2.855
    N134 X2.856
    N136 X2.857 Z-.0002
    N138 X2.858
    N140 X2.859 Z-.0003
    N142 X2.86
    N144 X2.861 Z-.0004
    N146 X2.862
    N148 X2.863
    N150 Z-.0005 #### Here ###
    N152 X2.864
    N154 X2.865
    N156 Z-.0006
    N158 X2.866
    N160 X2.867
    N162 Z-.0007 ######Here ######
    N164 X2.868
    N166 Z-.0008 ######Here ######
    N168 X2.869
    N170 Z-.0009 ######Here ######
    N172 X2.87
    N174 Z-.001
    N176 X2.871
    N178 X2.872
    N180 Z-.0011
    N182 X2.873
    N184 Z-.0012

    Thenks for all the help

    Reston

  12. #12
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Text file loop

    You want to add line number and "G04 P1" on the same line, or just add "G04 P1"? I'm guessing you don't want two line numbers on one line...
    VB Code:
    1. Dim sContent As String
    2.     Dim iInputFile As Integer
    3.     Dim iOutputFile As Integer
    4.     Dim iFirstSpace As Integer
    5.     Dim iLineNumber As Integer
    6.    
    7.     iInputFile = FreeFile()
    8.     Open "E:\MC\sometext.txt" For Input As #iInputFile
    9.     iOutputFile = FreeFile()
    10.     Open "E:\MC\anotherFile.txt" For Output As #iOutputFile
    11.     Do While Not EOF(iInputFile)
    12.         Line Input #iInputFile, sContent
    13.         iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
    14.         If Mid$(sContent, iFirstSpace + 1, 1) = "Z" Then ' Check the first character after the first space
    15.             sContent = sContent & " G04 P1" ' Add to the line
    16.         End If
    17.         Print #iOutputFile, sContent
    18.     Loop
    19.     Close #iInputFile
    20.     Close #iOutputFile

  13. #13

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

    Re: Text file loop

    Hi Look at my last post at line number 150 and see how that line is a Z move. I want a numberline and Go4 P1 just past that line also.

    This is what i came up with from your post.

    visual basic 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:\Documents and Settings\n\Desktop\U-CUT2.t" For Input As #iInputFile
    iOutputFile = FreeFile()
    Open "C:\Documents and Settings\n\Desktop\U-CUT3.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, 1) = "X" 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, "N" & iLineNumber & " G04 P1" ' Create and print the new line
    End If
    iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
    If Mid$(sContent, iFirstSpace + 1, 1) = "Z" 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, "N" & iLineNumber & " G04 P1" ' Create and print the new line


    End If
    Loop
    Close #iInputFile
    Close #iOutputFile

    End Sub
    -----------------------------------------
    This what the G-Code looks like before i use the code you helped me out with.
    %
    O0000
    (PROGRAM NAME - U-CUT2 )
    (DATE=DD-MM-YY - 19-06-06 TIME=HH:MM - 12:40 )
    N100 G20
    N102 G0 G17 G40 G49 G80 G90
    N104 G91 G28 Z0.
    N106 G91 G28 X0. Y0.
    N108 G92 X0. ? Y0. ? Z0. ?
    N110 G0 G90 G52 X2.847 Y0. S2000 M3
    N112 Z.25
    N114 Z.1
    N116 G1 Z0. F10.
    N118 X2.848
    N120 X2.849
    N122 X2.85
    N124 X2.851
    N126 X2.852
    N128 X2.853 Z-.0001
    N130 X2.854
    N132 X2.855
    N134 X2.856
    N136 X2.857 Z-.0002
    N138 X2.858
    N140 X2.859 Z-.0003
    N142 X2.86
    N144 X2.861 Z-.0004
    N146 X2.862
    N148 X2.863
    N150 Z-.0005
    N152 X2.864
    N154 X2.865
    N156 Z-.0006
    N158 X2.866
    N160 X2.867
    N162 Z-.0007
    N164 X2.868
    N166 Z-.0008
    N168 X2.869
    N170 Z-.0009
    N172 X2.87
    N174 Z-.001
    N176 X2.871
    N178 X2.872
    N180 Z-.0011
    N182 X2.873
    N184 Z-.0012
    N186 X2.874
    N188 Z-.0013
    N190 X2.875
    N192 Z-.0014
    N194 X2.8759
    N196 Z-.0015


    Hey so just wondering. What do you do that you know about VB?

    Thanks Reston

  14. #14

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

    Re: Text file loop

    And after the code has been run.


    %
    O0000
    (PROGRAM NAME - U-CUT2 )
    (DATE=DD-MM-YY - 19-06-06 TIME=HH:MM - 12:40 )
    N100 G20
    N102 G0 G17 G40 G49 G80 G90
    N104 G91 G28 Z0.
    N106 G91 G28 X0. Y0.
    N108 G92 X0. ? Y0. ? Z0. ?
    N110 G0 G90 G52 X2.847 Y0. S2000 M3
    N112 Z.25
    N113 G04 P1
    N114 Z.1
    N115 G04 P1
    N116 G1 Z0. F10.
    N118 X2.848
    N119 G04 P1
    N120 X2.849
    N121 G04 P1
    N122 X2.85
    N123 G04 P1
    N124 X2.851
    N125 G04 P1
    N126 X2.852
    N127 G04 P1
    N128 X2.853 Z-.0001
    N129 G04 P1
    N130 X2.854
    N131 G04 P1
    N132 X2.855
    N133 G04 P1
    N134 X2.856
    N135 G04 P1
    N136 X2.857 Z-.0002
    N137 G04 P1
    N138 X2.858
    N139 G04 P1
    N140 X2.859 Z-.0003
    N141 G04 P1
    N142 X2.86
    N143 G04 P1
    N144 X2.861 Z-.0004
    N145 G04 P1
    N146 X2.862
    N147 G04 P1
    N148 X2.863
    N149 G04 P1
    N150 Z-.0005
    N151 G04 P1
    N152 X2.864
    N153 G04 P1
    N154 X2.865
    N155 G04 P1
    N156 Z-.0006
    N157 G04 P1
    N158 X2.866
    N159 G04 P1
    N160 X2.867
    N161 G04 P1
    N162 Z-.0007
    N163 G04 P1
    N164 X2.868
    N165 G04 P1
    N166 Z-.0008
    N167 G04 P1
    N168 X2.869
    N169 G04 P1
    N170 Z-.0009
    N171 G04 P1
    N172 X2.87
    N173 G04 P1
    N174 Z-.001
    N175 G04 P1
    N176 X2.871
    N177 G04 P1
    N178 X2.872
    N179 G04 P1
    N180 Z-.0011
    N181 G04 P1
    N182 X2.873
    N183 G04 P1
    N184 Z-.0012
    N185 G04 P1
    N186 X2.874

  15. #15

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

    Re: Text file loop

    Hi All

    I had some help with this bit of code. the problem i'm having now is that my work mates want this to work on a file that is 150000 line of g-code. I'm getting an "RUN TIME ERROR 6"
    overflow

    This loops through 32766 line before i get this error.

    visual basic 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:\Documents and Settings\n\Desktop\U-CUT2.t" For Input As #iInputFile
    iOutputFile = FreeFile()
    Open "C:\Documents and Settings\n\Desktop\U-CUT3.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, 1) = "X" 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, "N" & iLineNumber & " G04 P1" ' Create and print the new line
    End If
    iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
    If Mid$(sContent, iFirstSpace + 1, 1) = "Z" 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, "N" & iLineNumber & " G04 P1" ' Create and print the new line


    End If
    Loop
    Close #iInputFile
    Close #iOutputFile

    End Sub
    -----------------------------------------

  16. #16
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Text file loop

    Quote Originally Posted by tiguy
    This loops through 32766 line before i get this error.
    You have hit the max an Integer can hold. Change all of your Integers to Longs.

  17. #17
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: Text file loop

    no need to change iInputFile and iOutputFile to Long

  18. #18
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Text file loop

    Quote Originally Posted by Agilaz
    no need to change iInputFile and iOutputFile to Long
    I never use Integers for anything out of habit because I have been bitten by this situation one too many times.

    Even if I need to loop through 5 items, I still use a Long.

  19. #19

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

    Re: Text file loop

    Hey that did not work. i'm still getting the same error.

    Any help?

  20. #20
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: Text file loop

    Quote Originally Posted by Hack
    I never use Integers for anything out of habit because I have been bitten by this situation one too many times.

    Even if I need to loop through 5 items, I still use a Long.
    but freefile() always returns a Integer so there's no need to let VB convert it to Long

    @tiguy

    change all cInt() to cLng()

  21. #21

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

    Re: Text file loop

    You guy's are killer. that work like a charm.

    Thank you
    Reston

  22. #22

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

    Re: Text file loop

    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 & " G04 P1" ' Create and print the new line
    End If


    Loop
    Close #iInputFile
    Close #iOutputFile

    End Sub

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