Remove a character from a text file using .bat

adeline_jolie Posted messages 2 Status Member -  
 Dev1 -
Hello everyone,

I have a text file (prog.txt) that is as follows:

N5 x50.
N10 y25.
N1000 g50

and I would like to create a .bat file to

1) remove "all words starting with Nxxxx"
2) remove all periods at the end of lines
3) remove undesirable characters like g50

The final file would look like:

x50
y25

Thank you in advance for your experience

4 answers

  1. bricomachin Posted messages 245 Registration date   Status Member Last intervention   134
     
    Hi,

    Here's a code that should help you correct your file...
    @echo off

    if exist "progCorrige.txt" (
    color 0c
    echo Error: the file progCorrige.txt already exists!
    pause
    exit
    )

    if not exist "prog.txt" (
    color 0c
    echo Error: the file prog.txt does not exist!
    pause
    exit
    )

    setlocal ENABLEDELAYEDEXPANSION

    set line=""

    REM split each line of the text file
    for /f "delims=" %%a in ('type prog.txt') do (

    set line=%%a
    REM This line removes the period at the end of the line if it exists
    if "!line:~-1!"=="." set line=!line:~0,-1!

    rem word-by-word split
    for /f "tokens=1*" %%b in ("!line!") do (

    set lineNumber=%%b

    REM if the first word starts with N, we don't take it into account
    if "!lineNumber:~0,1!"=="N" set line=%%c

    )
    echo !line!>>progCorrige.txt

    if exist "progCorrige.txt" (
    color 0A
    echo The corrected file has been created!
    pause
    exit
    )
    )

    pause

    endlocal



    It's ISO code in your file if I'm not mistaken, what do you do? milling, turning, or something else? Just curious ^^

    @+

    Bricomachin
    --
    A little thank you is always appreciated as well as proper spelling.
    Posts also deserve a retirement! Remember to mark your topic as resolved :p
    1
    1. bricomachin Posted messages 245 Registration date   Status Member Last intervention   134
       
      However, I realize that there is a small encoding problem that means the messages will have no accents...
      0
    2. adeline_jolie Posted messages 2 Status Member
       
      Hello Bricomachin,

      Thanks for advancing me a bit more.... it's still not working... but we're getting closer.... lol

      the final file doesn't look like:
      x50
      y25

      But you've given me a good starting point.... lol
      I didn't understand setlocal ENABLEDELAYEDEXPANSION

      Would you have some time to complete it.... Thanks in advance
      0
    3. Dev1
       
      Hello bricomachin

      I have a problem that resembles Aadeline_jolie's

      I have a file whose lines look like this
      "id"; "user"; ''login"; ....

      I would like to know if it is possible to remove all the quotes from each line.

      Thank you for your help
      0
  2. bricomachin Posted messages 245 Registration date   Status Member Last intervention   134
     
    Hi,

    First of all, I’m sorry for not seeing your message earlier.
    I tested the file at home and it only gives me x50. It’s weird because I managed to format the file well once...

    Well, I will modify the file again and send it back to you.

    And just so you know, setlocal ENABLEDELAYEDEXPANSION is a very useful instruction for solving SET problems in FOR and IF statements, for example.
    In this case, I use it to be able to perform a SET in a loop without always getting the first value returned.

    Here's a little example for you to see:
    @echo off

    echo the set in the if does not work
    set test=Fail
    if 1==1 (
    set test=Works
    echo test : %test%
    pause
    )

    echo.
    echo.
    echo but outside the if it works
    set test2=Fail
    if 1==1 (
    set test2=Works
    )
    echo test2 : %test2%
    pause

    setlocal ENABLEDELAYEDEXPANSION

    echo.
    echo.
    echo the set in the if works with setlocal ENABLEDELAYEDEXPANSION
    set test3=Fail
    if 1==1 (
    set test3=Works
    echo test3 : !test3!
    pause
    )

    endlocal

    Okay, I’ll get to it tomorrow at work, I think... but I might only have the opportunity to do it on Friday.

    In any case, you can already brush up on this wonderful command setlocal ENABLEDELAYEDEXPANSION ^^

    @++

    Bricomachin
    --
    A little thank you is always appreciated as well as correct spelling.
    Posts also deserve retirement! Remember to mark your subject as resolved :p
    0
  3. bricomachin Posted messages 245 Registration date   Status Member Last intervention   134
     
    Hello,

    First of all, if a Batch programmer comes by, it would be cool if they could explain why in the code posted above the cause of the problem is the
    echo !ligne!>>progCorrige.txt
    . Indeed, after looooong hours of rewriting the program a hundred times and disabling one command at a time to find out which one was causing the issue, I understood that if you put
    echo !ligne!
    it works (it displays x50 y25 g50) but with
    echo !ligne!>>progCorrige.txt
    it only writes x50 in the file!!!

    Well then, to be able to write the ISO code in the file progCorrige.txt, you need to call "traitement ISO.bat" in the cmd like this:
    "traitement ISO.bat"> progCorrige.txt 2>&1
    or launch a batch file that will call "traitement ISO.bat" and contains this:
    @echo off 
    call "traitement ISO.bat" >progCorrige.txt 2>&1
    exit

    But at that moment, it creates progCorrige before launching "traitement ISO.bat" and therefore we receive the error of the file already existing in progCorrige.txt.

    In conclusion:

    Use the program:
    @echo off 
    if not exist "prog.txt" (
    color 0c
    echo Error: the file prog.txt does not exist!
    pause
    exit
    )
    setlocal ENABLEDELAYEDEXPANSION
    set ligne=""
    REM split each line of the text file
    for /f "delims=" %%a in (prog.txt) do (
    set ligne=%%a
    REM This line removes the period at the end of the line if it exists
    if "!ligne:~-1!"=="." set ligne=!ligne:~0,-1!
    rem split word by word
    for /f "tokens=1* delims= " %%b in ("!ligne!") do (
    set noLigne=%%b
    REM if the 1st word starts with N, we don't take it into account
    if "!noLigne:~0,1!"=="N" set ligne=%%c
    )
    echo !ligne!
    )
    endlocal

    and name it "traitement ISO.bat"

    Then launch it using the following program:
    @echo off 
    call "traitement ISO.bat" >progCorrige.txt 2>&1
    exit

    which can be named whatever you want...

    I'm sorry for the long message, but this was a problem I had never encountered before and I had a hard time solving it ^^.

    Still, I'm happy to have come across this post, if only to enrich my knowledge.

    Anyway, I remain at your disposal for any other modifications/questions and wish you a good evening.

    Bricomachin

    --
    A small thank you is always appreciated as well as correct spelling.
    Posts also deserve retirement! Please consider marking your topic as resolved :p
    0
    1. bricomachin Posted messages 245 Registration date   Status Member Last intervention   134
       
      I'm in the process of modifying the code to remove the g50 etc and keep only the coordinates...
      0
  4. bricomachin Posted messages 245 Registration date   Status Member Last intervention   134
     
    Hello,

    Here are two versions of code that only keep the coordinates:
    the first one if the x and y can be at variable places in the line
    example:
    N10 x50 g50 then on the next line:
    N15 g03 y80

    @echo off 
    if not exist "C:\Users\Romain\Desktop\prog.txt" (
    color 0c
    echo Error: the file prog.txt does not exist!
    pause
    exit
    )
    setlocal ENABLEDELAYEDEXPANSION
    set line=""
    set finalLine=
    REM split each line of the text file
    for /f "delims=" %%a in (prog.txt) do (
    set line=%%a
    REM This line removes the period at the end of the line if it exists
    if "!line:~-1!"=="." set line=!line:~0,-1!
    set finalLine=
    rem split word by word (I don't think there can be more than 6 arguments on a line...)
    for /f "tokens=1-6 delims= " %%b in ("!line!") do (

    set lineNum=%%b
    rem echo !Line!
    REM if the word does not start with X or Y, it is not taken into account
    if /i "!lineNum:~0,1!"=="X" (
    set finalLine=!lineNum!
    ) else (
    if /i "!lineNum:~0,1!"=="Y" (
    set finalLine=!lineNum!)
    )
    set lineNum=%%c
    REM if the word does not start with X or Y, it is not taken into account
    if /i "!lineNum:~0,1!"=="X" (
    set finalLine=!lineNum!
    ) else (
    if /i "!lineNum:~0,1!"=="Y" (
    set finalLine=!lineNum!)
    )
    set lineNum=%%d
    REM if the word does not start with X or Y, it is not taken into account
    if /i "!lineNum:~0,1!"=="X" (
    set finalLine=!lineNum!
    ) else (
    if /i "!lineNum:~0,1!"=="Y" (
    set finalLine=!lineNum!)
    )
    set lineNum=%%e
    REM if the word does not start with X or Y, it is not taken into account
    if /i "!lineNum:~0,1!"=="X" (
    set finalLine=!lineNum!
    ) else (
    if /i "!lineNum:~0,1!"=="Y" (
    set finalLine=!lineNum!)
    )
    set lineNum=%%f
    REM if the word does not start with X or Y, it is not taken into account
    if /i "!lineNum:~0,1!"=="X" (
    set finalLine=!lineNum!
    ) else (
    if /i "!lineNum:~0,1!"=="Y" (
    set finalLine=!lineNum!)
    )
    set lineNum=%%g
    REM if the word does not start with X or Y, it is not taken into account
    if /i "!lineNum:~0,1!"=="X" (
    set finalLine=!lineNum!
    ) else (
    if /i "!lineNum:~0,1!"=="Y" (
    set finalLine=!lineNum!)
    )
    )
    if not "!finalLine!"=="" echo !finalLine!
    )

    endlocal
    exit

    and this one if you know it will always be the first argument
    example:
    N20 y80 g50
    N25 x45 g03

    @echo off 
    if not exist "C:\Users\Romain\Desktop\prog.txt" (
    color 0c
    echo Error: the file prog.txt does not exist!
    pause
    exit
    )
    setlocal ENABLEDELAYEDEXPANSION
    set line=""
    set finalLine=
    REM split each line of the text file
    for /f "delims=" %%a in (prog.txt) do (
    set line=%%a
    REM This line removes the period at the end of the line if it exists
    if "!line:~-1!"=="." set line=!line:~0,-1!
    set finalLine=
    rem split word by word (I don't think there can be more than 6 arguments on a line...)
    for /f "tokens=1,2 delims= " %%b in ("!line!") do (

    set lineNum=%%b
    rem echo !Line!
    REM if the word does not start with X or Y, it is not taken into account
    if /i "!lineNum:~0,1!"=="X" (
    set finalLine=!lineNum!
    ) else (
    if /i "!lineNum:~0,1!"=="Y" (
    set finalLine=!lineNum!)
    )
    set lineNum=%%c
    REM if the word does not start with X or Y, it is not taken into account
    if /i "!lineNum:~0,1!"=="X" (
    set finalLine=!lineNum!
    ) else (
    if /i "!lineNum:~0,1!"=="Y" (
    set finalLine=!lineNum!)
    )

    )
    if not "!finalLine!"=="" echo !finalLine!
    )

    endlocal

    exit



    there you go,

    @++

    Bricomachin
    --
    A little thank you is always appreciated as well as good spelling.
    Posts also deserve a retirement! Remember to mark your topic as resolved :p
    0