Home > Back-end >  Batch removing X number of lines from file from Nth line
Batch removing X number of lines from file from Nth line

Time:06-06

I am trying to remove a tag from a set of html files and I can't seem to find the right commands to do the job.

I am able to find the line number of the tag; the tag is a total of 10 lines that need to be deleted. Once I find this line, how do I got and delete said line & next 10 lines?

Here's what I have (a first for loop to collect files, a second for loop that collects all line numbers.) thank you.

::start by creating a list of html files
set i=0
for /r %%G in (*.html) do (
    set /A i =1
    set array[!i!]=%%G
)
set n=%i%

::second nested loop collects all lines for bottom secion to be deleted.
set j = 0
for /L %%i in (1,1,%n%) do (
     for /f "delims=" %%a in ('findstr /n /c:"u-backlink u-clearfix u-grey 80" !array[%%i]!') do (
         set var=%%a
         set /A j =1
         set array[!j!]=!var:~0,3!
     )
)
set m=%j%

CodePudding user response:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"

for /r "%sourcedir%" %%G in (*.html) do (
 SET "linecount="
 FOR /f "delims=:" %%e IN ('findstr /n /c:"u-backlink u-clearfix u-grey 80" "%%G"') DO SET /a linecount=%%e
 IF DEFINED linecount (
  FOR /f "usebackqdelims=" %%b IN ("%%G") DO (
   SET /a linecount-=1
   IF !linecount! gtr 0 ECHO %%b
   IF !linecount! lss -10 ECHO %%b
  )
 )>"           
  • Related