批次从第n行中删除文件数量的x行数

发布于 2025-02-04 06:44:53 字数 617 浏览 2 评论 0原文

我正在尝试从一组HTML文件中删除标签,但似乎找不到正确的命令来完成这项工作。

我能够找到标签的行号;该标签总共需要删除10行。一旦找到了这条线,我将如何获得并删除所说的行&接下来的10行?

这是我拥有的(循环收集文件的第一个,第二个收集所有行号的循环。)谢谢。

::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%

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%

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2025-02-11 06:44:53
@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

sourcedir启动目标文件的递归目录列表。

初始化linecount to nothing 并使用findstr来定位目标字符串。 %% e将设置为找到的行数,set将其设置为linecount

如果找到字符串,linecount现在将包含一个值,因此请阅读每行并计算。如果linecount在0和-10之间,则我们不echo输出文件的行。

我最初将所有生成的文件都放在一个目录中。您要随意更改。

修订以生成一个临时文件(,其中将是无关紧要的),然后将该文件移到原始文件上。

在应用真实数据之前,请务必针对测试目录进行验证。

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文