批处理 - 获取标志之间的文本块,输出并遍历所有文件

Batch - Get block of text between flags, output and iterate over all files

我在一个目录中有一堆文本文件,其中有一段文本,我想将两个字符串之间的文本提取到一个具有相似名称的新文本文件中。我已经让单个文件正常工作,但认为我已经摆脱了循环遍历所有 .txt 文件的困境。也许在“goto”命令下? 这是我使用的原始单文件代码: Batch File - Find two lines then copy everything between those lines

~Top Break
foobar
~ more data title
more foobar 
~Bottom Break
Garbage data

我的代码适用于名为 FileNumber1.txt.

的单个文件
@echo off

    set "FIRSTLINE=~Top Break"
    set "LASTLINE=~Bottom Break"
    set "INFILE=FileNumber1.txt"
    setlocal EnableExtensions DisableDelayedExpansion
    set "FLAG="
    > "%INFILE%_MyData.txt" (
        rem findstr configured so that each line in a  file is given a "1:" number and colon.
        for /F "delims=" %%L in ('findstr /N "^" "%INFILE%"') do (
            set "LINE=%%L"
            setlocal EnableDelayedExpansion
        rem this LINE=!LINE:*:=! removes the any character before the Colon. *:
            set "LINE=!LINE:*:=!"
        rem this block of code checks to see if line of text = Firstline variable, if so FLAG = TRUE
            if "!LINE!"=="%FIRSTLINE%" (
                endlocal
                set "FLAG=TRUE"
        rem this block of code checks to see if line of text = Lastline variable, if so goto :Continue and end the loop
            ) else if "!LINE!"=="%LASTLINE%" (
                endlocal
                goto :CONTINUE
            ) else if defined FLAG (
                echo(#!LINE!
                endlocal
            ) else (
                endlocal
            )
        )
    )
    :CONTINUE
    endlocal
    
    
    
   

NewFile1_MyData.txt 输出:

foobar
~ more data title
more foobar 

我试图将其包装在另一个“FOR”循环中,该循环在同一目录中查找所有 txt 文件。 这是我的代码,但无法正常工作。

@echo off

set "FIRSTLINE=~Top Break"
set "LASTLINE=~Bottom Break"

for /F %%f in (*.txt) do (
set "INFILE=%%f"
setlocal EnableExtensions DisableDelayedExpansion
set "FLAG="
> "%INFILE%_OldHeader.txt" (
    rem findstr configured so that each line in a  file is given a "1:" number and colon.
    for /F "delims=" %%L in ('findstr /N "^" "%INFILE%"') do (
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
    rem this LINE=!LINE:*:=! removes the any character before the Colon. *:
        set "LINE=!LINE:*:=!"
    rem this block of code checks to see if line of text = Firstline variable, if so FLAG = TRUE
        if "!LINE!"=="%FIRSTLINE%" (
            endlocal
            set "FLAG=TRUE"
    rem this block of code checks to see if line of text = Lastline variable, if so goto :Continue and end the loop
        ) else if "!LINE!"=="%LASTLINE%" (
            endlocal
            goto :CONTINUE
        ) else if defined FLAG (
            echo(#!LINE!
            endlocal
        ) else (
            endlocal
        )
    )
)

endlocal


:CONTINUE
))

命令 window 到达“for /F”语句并退出。

嗯...我会根据文件开头要跳过的行和要提取的行数来更改提取行的方法,以获得更简单的方法。之后,我将使用 for 来处理所有文件,并使用 call 子程序来提取行:

@echo off
setlocal EnableDelayedExpansion

set "FirstLine=~Top Break"
set "LastLine=~Bottom Break"

rem Process all text files in this folder
for %%f in (*.txt) do (

   rem Search for First line and Number of lines
   set "FirstNum="
   for /F "delims=:" %%n in ('findstr /C:"%FirstLine%" /C:"%LastLine%" /N "%%f"') do (
      if not defined FirstNum (
         set "FirstNum=%%n"
      ) else (
         set /A "LastNum=%%n-FirstNum-1"
      )
   )

   rem Copy the lines
   call :CopyLines >"%%~Nf_MyData.out" "%%f", !FirstNum!, !LastNum!  

)
ren *.out *.txt
goto :EOF


:CopyLines File, Skip, Num
set "Num=%3"
for /F "usebackq skip=%2 delims=" %%a in (%1) do (
   setlocal DisableDelayedExpansion
   echo %%a
   endlocal
   set /A Num-=1
   if !Num! equ 0 exit /B
)
exit /B