Search code examples
windowsbatch-filefindstr

Delete first few lines from a text file with Windows batch scripting


Preferably a one-liner, how could I delete a range of lines at the beginning from a large (3MB+) text file in a timely fashion (few seconds max). I've seen solutions using for /f along with findstr, but the for loop made it extremely slow, and the tool more cannot handle larger files without hanging.

@echo off &setlocal
set "testing.txt=%~1"
(for /f "delims=" %%i in ('findstr /n "^" "testing.txt"') do (
    set "line=%%i"
    for /f "delims=:" %%a in ("%%i") do set "row=%%a"
    setlocal enabledelayedexpansion
    set "line=!line:*:=!"
    if !row! gtr 100 echo(!line!
    endlocal
))>output.txt

Here is an attempt. It is incredibly slow. Any recommendations would be appreciated.


Solution

  • This is the fastest way to eliminate the first lines in a large file. Is written as one-liner, as you requested:

    @echo off
    < testing.txt ( (for /L %%i in (1,1,100) do set /P "=") & findstr "^" ) > output.txt
    

    However, be aware that this method can only manage lines up to 1023 characters long because it uses set /P command to read and discard not desired lines...

    For a description of this method, see this answer.