how do i DELETE a line?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

how do i DELETE a line?

#1 Post by nnnmmm » 09 Jan 2022 07:51

i finished the batch that could erase one line but not delete

erase line 2
before
1
2
3
4
5
-------------
after
1

3
4
5

Code: Select all

::not DELETE, but ERASE one line of your choice

@echo off &setlocal

rem set "file=%~1"
set "file=c:\a ! & bc.txt"

set /A DELETE=5
set /A KEEP1=DELETE-1
set /A KEEP2=DELETE+1

IF /I %DELETE%==0 GOTO :EOF

setlocal EnableDelayedExpansion
<"!file!" >"!file!.~tmp" (
   for /f %%i in ('type "!file!"^|find /c /v ""') do (

      for /L %%A in (1 1 %KEEP1%) do (
         set "line1="& set /p "line1="
         echo(!line1!
      )

      for /L %%A in (%DELETE% 1 %DELETE%) do (
         set "line2="& set /p "line2="
         echo(
      )

      for /L %%A in (%KEEP2% 1 %%i) do (
         set "line3="& set /p "line3="
         echo(!line3!
      )

   )
)
>nul move /y "!file!.~tmp" "!file!"
endlocal

how do i make the echo DELETE the line.
for /L %%A in (%DELETE% 1 %DELETE%) do (
set "line2="& set /p "line2="
echo(

DELETE line 2
before
1
2
3
4
5
----
after
1
3
4
5

question 2
set /A SKIP=4
for /l %%j in (1+SKIP 1 %%i) is ok

set /A DELETE=5
for /L %%A in (DELETE 1 DELETE) is not ok, why?
numerical number + string number isnt an error?

to save your time, i answered with few choices
1. that is just the way it is, accept it
2. doing it so has unknown benefits for heavy users
3. batch logic is a trash

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do i DELETE a line?

#2 Post by aGerman » 09 Jan 2022 08:41

Something about like that:

Code: Select all

<"!file!" >"!file!.~tmp" (
  for /f %%i in ('type "!file!"^|find /c /v ""') do for /L %%A in (1 1 %%i) do (
    REM you always have to read *every* line
    set "line="& set /p "line="
    REM but write lines selectively
    if %%A neq 2 echo(!line!
  )
)
Steffen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: how do i DELETE a line?

#3 Post by nnnmmm » 11 Jan 2022 11:09

>>for /l %%j in (1+SKIP 1 %%i) is ok
this wasnt ok, i found it later, the file had many many trailing invisible empty lines because of it. so they were string numbers, i wrestled with it for many hours. i was able to get the erase one. thanks for the help of delete one, now i have insert, erase and delete lines.

Post Reply