Reputation: 41
I am writing a VBS script where a G-Code file is dropped onto the script file and a particular line in the file is being replaced. I am trying to figure out how to tell my script to find one string, in my case ( Tapping )
and then look for the very last string M3
in my case and replace it with M845
. Here is what I have so far.
My G-Code file that will be dragged and dropped on the script file:
O1 ( POSTED FILE NAME - WORKNC POST )
N1 ( INPUT FILE NAME - _6263_Core_ROUGH_03.TBA)
N2 (DATE/TIME: Tue Mar 13 12:16:07 2018)
N3 G0 G40 G80 G90 G98
N4 G17
N5 G57H904
N6 G173W0.0
N7 B0.000
N8 (Drill 0.005000)
N9 T19
N10 M06
N11 S5000
N12 M3
N13 G173 W0.0
N14 (- )
N15 ( Contour Chamfer )
N16 G0 G90 X-0.0003 Y12.1092
N17 G43 Z10.2793 H19 M08
N18 G01 F500. X-0.0003 Y12.1092
N19 G01 Z10.5907 F500.
N20 Z10.2001
N21 X-0.0003 Y12.1092 Z8.1808
N22 G01 Z7.9014 F65.
N23 G3 X0.092 Y12.0168 I0.0924 J0.
N24 G3 X0.1844 Y12.1092 I-0. J0.0924
N25 G3 X0.0246 Y12.2923 I-0.1848 J-0.
N26 G3 X-0.1482 Y11.9985 I-0.0249 J-0.1831
N27 G3 X0.1823 Y12.0816 I0.1479 J0.1108
N28 G01 X0.1844 Y12.1092
N29 G3 X0.092 Y12.2016 I-0.0924 J-0.
N30 G3 X-0.0003 Y12.1092 I0. J-0.0924
N31 G01 Z8.2001
N32 G01 Z10.4001 F500.
N33 X-0.0003 Y12.1092 Z10.5907
N34 M09
N35 M05
N36 G91 G28 Z0
N37 G90
N38 G57H904
N39 G173W0.0
N40 B0.000
N41 (Tapper 0.500000)
N42 T23
N43 M06
N44 S130
N45 M3
N46 G173 W0.0
N47 (- )
N48 ( Tapping )
N49 G0 G90 X-0.0003 Y12.1092
N50 G43 Z10.5907 H23 M08
N51 G01 F500. X-0.0003 Y12.1092
N52 G01 Z10.2793 F500.
N53 G98 G84 X-0.0003 Y12.1092 Z6.8692 R8.2001 F10.
N54 G80 G01 F500.
N55 X-0.0003 Y12.1092 Z10.2793
N56 X-0.0003 Y12.1092 Z10.2793
N57 M09
N58 M05
N59 G91 G28 Z0
N60 G90
N61 M30
Now, this file is auto-generated by a CAM program, and can have more sections, depending on what is being done by the program.
What I am looking to accomplish is to find ( Tapping )
and replace the line 3 lines before that, in this case N45 M3
, with N45 M845
. I can't have it replace every instance of M3
though as there are more of them that need to stay. I just need to replace the M3
that comes before the ( Tapping )
.
Here is what I have so far for my script:
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = "( Tapping )" - 3 'I have no idea?
strNewText = "N45 M845"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close
Note: The N1,N2,N3,etc. are not really of importance. So when I try to replace the line that contains M3
I will just throw a random N and number before it. It's not actually of much importance.
I was looking into searching for the M3
between two strings. For instance between Tapper
and Tapping
, but I couldn't seem to get that to work properly.
I would appreciate any input to put me on the right path here. Thanks!
Upvotes: 0
Views: 86
Reputation: 175766
One way:
...
'// split file into array of lines
dim lines: lines = split(objFile.ReadAll, vbCrLf)
dim lastSeenM3: lastSeenM3 = -1
dim lastSeenTapping: lastSeenTapping = -1
dim i
for i = 0 to ubound(lines)
'// if line ends with " M3" save its index
if right(lines(i), 3) = " M3" then
lastSeenM3 = i
elseif instr(lines(i), strOldText) then
'// line contains "( Tapping )"
lastSeenTapping = i
'// overwite the last M3
if (lastSeenM3 >= 0) then
lines(lastSeenM3) = strNewText
else
'// no previous M3 ...
end if
elseif right(lines(i), 4) = " M05" then
if (lastSeenTapping >= 0) then
'// an M5 after tapping
lines(i) = "this replaces all M5 lines after tapping"
end if
end if
next
'// turn array into string, write back to disk
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write join(lines, vbCrLf)
objFile.Close
Upvotes: 1