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%
@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
)
)>"%destdir%\tempfile"& MOVE /y "%destdir%\tempfile" "%%G" >nul
)
GOTO :EOF
Start a recursive directory list of the target files from sourcedir
.
Initialise linecount
to nothing and use findstr
to locate the target string. %%e
will be set to the number of the line found, set
this into linecount
.
If the string was found, linecount
will now contain a value, so read each line and count down. If linecount
is between 0 and -10, we don't echo
the line to the output file.
I originally put all the generated files in the one directory. Yours to change at will.
Revised to generate a temporary file (quite where would be irrelevant) and then move that file over the original.
Always verify against a test directory before applying to real data.