Windows command processor designed for executing commands and executables and not for text file processing is definitely the worst choice to filter TEST.LOG
. For the reasons read completely my answer on How to read and print contents of text file line by line? The batch file code described there in detail was used as template for the batch file code below:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist "Test.log" goto EndBatch
set "OutputLines="
(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "Test.log"') do (
set "Line=%%I"
setlocal EnableDelayedExpansion
if defined OutputLines (
echo(!Line:*:=!
if not "!Line:</Report>=!" == "!Line!" (
endlocal & set "OutputLines="
) else endlocal
) else if not "!Line:<Line Text=!" == "!Line!" (
echo(!Line:*:=!
endlocal & set "OutputLines=1"
) else endlocal
))>"newfile.txt"
if exist "newfile.txt" for %%I in ("newfile.txt") do if %%~zI == 0 del "newfile.txt"
:EndBatch
endlocal
This batch file writes all lines from a line containing case-insensitive the string <Line Text
to a line containing case-insensitive the string </Report>
or end of file from Test.log
into file newfile.txt
.
Note: The search string between !Line:
and =
cannot contain an equal sign because of the equal sign is interpreted by Windows command processor as separator between search string, here </Report>
and <Line Text
, and the replace string, here twice an empty string. And an asterisk *
at beginning of search string is interpreted by Windows command processor as order to replace everything from beginning of line to first occurrence of found string on doing the string substitution and not as character to find in the line. But this does not matter for this use case.
If the two lines marking beginning and end of block to extract are fixed and do not contain any variable part, the two string comparisons could be done without string substitution making it possible to compare also strings containing an equal sign.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist "Test.log" goto EndBatch
set "BlockBegin= <Line Text="***********TEST1 TEST TEST************" />"
set "BlockEnd= </Report>"
set "OutputLines="
(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "Test.log"') do (
set "Line=%%I"
setlocal EnableDelayedExpansion
if defined OutputLines (
echo(!Line:*:=!
if "!Line:*:=!" == "!BlockEnd!" (
endlocal & set "OutputLines="
) else endlocal
) else if "!Line:*:=!" == "!BlockBegin!" (
echo(!Line:*:=!
endlocal & set "OutputLines=1"
) else endlocal
))>"newfile.txt"
if exist "newfile.txt" for %%I in ("newfile.txt") do if %%~zI == 0 del "newfile.txt"
:EndBatch
endlocal
This variant compares each entire line case-sensitive with the strings assigned to the environment variables BlockBegin
and BlockEnd
to determine on which line to start and on which line to stop the output of the lines.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
echo /?
endlocal /?
findstr /?
for /?
goto /?
if /?
set /?
setlocal /?
See also: