Question

Can anyone provide a windows command or batch script that will remove the top two rows from a text file?

I'd like to remove the first two lines of a text file using findstr. I can't use the "more +2 file.txt > newfile.txt" trick described here because i'm calling this via xp_cmdshell and that doesn't seem to work (process often hangs and creates a permanently locked file I can't delete).

I've heard that findstr can use regular expressions to find something like this, and i've seen some attempts using a bat file. I'm open to either one of these possibilities but haven't found a working solution yet.

Was it helpful?

Solution

try this:

@echo off &setlocal
set "myfile=%~1"
(for /f "delims=" %%i in ('findstr /n "^" "%myfile%"') do (
    set "line=%%i"
    for /f "delims=:" %%a in ("%%i") do set "row=%%a"
    setlocal enabledelayedexpansion
    set "line=!line:*:=!"
    if !row! gtr 2 echo(!line!
    endlocal
))>newfile
type newfile

btw. syntax for more:

<file>newfile more +2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top