我正在编写一个VBS脚本,其中G-Code文件被放到脚本文件中,并且正在替换文件中的特定行。我试图找出如何告诉我的脚本找到一个字符串,在我的情况下( Tapping )
,然后在我的情况下查找最后一个字符串M3
并将其替换为M845
。这是我到目前为止所拥有的。
将在脚本文件中拖放的我的G代码文件:
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
现在,此文件由CAM程序自动生成,并且可以包含更多部分,具体取决于程序执行的操作。
我要完成的是找到( Tapping )
并在此之前替换第3行,在本例中为N45 M3
,并使用N45 M845
。我不能替换M3
的每个实例,因为它们中有更多需要保留。我只需要替换M3
之前的( Tapping )
。
以下是我目前为我的剧本所做的事情:
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
注:N1,N2,N3等。并不重要。因此,当我尝试替换包含M3
的行时,我将在它之前抛出一个随机的N和数字。它实际上并不重要。
我正在寻找两个字符串之间的M3
。例如,在Tapper
和Tapping
之间,但我似乎无法正常工作。
我很感激任何投入让我走上正确的道路。谢谢!
答案 0 :(得分:1)
一种方式:
...
'// 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