'Batch file for removing certain information in a line
I have a gcode file .nc What I want to do with a batch file is remove all line numbering i.e. N1 *N2 N3 etc. I also want to remove the M00 at the start. Do you have any idea on how to do this? I tried several codes but keep ending up with bad results. Any help much appreciated.
N1 G90 G54 G00 Z25. M00
*N2 (5MM CRB 4FL 16 LOC)
N3 T01 M06
N4 T02
N5 S12000 M03
N6 G54
N7 M08
N8 X125. Y100.
N9 Z2.5
N10 G01 Z-2.5 F822.96
N11 X125.25 F1645.92
N12 G03 X125. Y100.25 I-.25 J0 F3291.84
N13 X124.75 Y100. I0 J-.25
N14 X125. Y99.75 I.25 J0
N15 X125.25 Y100. I0 J.25
N16 G01 X127.25
N17 G03 X125. Y102.25 I-2.25 J0
N18 X122.75 Y100. I0 J-2.25
N19 X125. Y97.75 I2.25 J0
N20 X127.25 Y100. I0 J2.25
N21 G01 X129.25
N22 G03 X125. Y104.25 I-4.25 J0
N23 X120.75 Y100. I0 J-4.25
N24 X125. Y95.75 I4.25 J0
N25 X129.25 Y100. I0 J4.25
N26 G01 X131.25
N27 G03 X125. Y106.25 I-6.25 J0
N28 X118.75 Y100. I0 J-6.25
N29 X125. Y93.75 I6.25 J0
N30 X131.25 Y100. I0 J6.25
N31 G01 X133.25
N32 G03 X125. Y108.25 I-8.25 J0
N33 X116.75 Y100. I0 J-8.25
N34 X125. Y91.75 I8.25 J0
N35 X133.25 Y100. I0 J8.25
N36 G01 X135.25
N37 G03 X125. Y110.25 I-10.25 J0
Here is my attempt which only returns line numbers that I don't want:
H@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
rename test.nc test.tmp
for /f %%a in (test.tmp) do (
set foo=%%a
if !foo!=="N" set foo=" "
echo !foo! >> test.nc)
del test.temp
Here is my finnished script for those who need it. Sorry about the norwegian text. The script just checks folder for .nc files. It only accepts one .nc file in the same folder as the script before runnning.
@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
rem stores date and time to variables
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
rem counting number of .nc files to a variable VAR
for /f %%i in ('dir *.nc /s /b 2^> nul ^| find "" /v /c') do set VAR=%%i
echo .nc file count %VAR%
rem checking if only one .nc file is the directory. Cancelling otherwise.
set n1=%VAR%
set n2=1
if %n1% lss %n2% (
echo Mangler .nc fil
pause
exit /b
) else if %n1% gtr %n2% (
echo For mange nc. filer i mappen. Kun tillatt med en .nc fil i mappen.
pause
exit /b
) else if %n1% equ %n2% (
echo Ok. Riktig antall filer i mappen.
)
pause
rem removes first word/token from each line and also removes M00 unless there is only one word on the line (then it replaces it with M00=). Be aware!
for /f "tokens=1*" %%i in ('type "*.nc"') do (
set "remainder=%%j"
echo !remainder:M00=! >> "%datestamp%-%timestamp%-scrubbed.nc"
)
Solution 1:[1]
You can use delims=
and tokens=
to assign substrings to metavariables:
I used filename.nc
as the demo file:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1*" %%i in ('type "filename.nc"') do (
set "remainder=%%j"
echo !remainder:M00=!
)
It might also be that you need to replace . M00
instead of just M00
. Do you do stop the program elsewhere in the G-Code other than line 1?
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1*" %%i in ('type "filename.nc"') do (
set "remainder=%%j"
echo !remainder:. M00=!
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |