Your code uses three Batch file constructs that are inherently slow: call
command, >>
append redirection and setlocal/endlocal
, and these constructs are executed once per each file line! It would be faster to include the subroutine into the original code to avoid the call
and setlocal
commands, and an echo !line!>>target.txt
command imply open the file, search for the end, append the data and close the file, so it is faster to use this construct: (for ...) > target.txt
that just open the file once. An example of a code with such changes is in Compo's answer.
This is another method to solve this problem that may run faster when the search line is placed towards the beginning of the file:
@echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('findstr /n "AXX0000XXXA" "C:Users23456Desktopdata.txt"') do (set /A find_line=%%a-1)
call :processFile < "C:Users23456Desktopdata.txt" > target.txt
goto :EOF
:processFile
rem Duplicate the first %find_line%-1 lines
for /L %%i in (1,1,%find_line%) do (
set /P "line="
echo !line!
)
rem Insert the additional line
type temp.txt
rem Copy the rest of lines
findstr ^^
exit /B
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…