Delete 1st 4 lines & last 8 Lines in TXT File

July 13, 2009 at 02:28:50
Specs: Windows XP

Can anyone help with a BAT file which will open a txt file and delete the first four lines and the last five lines. The text file has a header and footer which needs to be removed:

Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

I have this code which deletes the first four lines (see below) does anyone have something I can add to it to delete the last 8 lines?

Thanks in advance,


Code to delete the first 4 lines:

@echo off
setLocal EnableDelayedExpansion

if exist *.new del *.new

for /f "tokens=* delims= " %%A in ('dir /b *.txt') do (
set NAME=%%~nA
set fileNAME=%%A
for /f "tokens=* skip=4 delims= " %%L in (!fileNAME!) do (
echo %%L >> !NAME!.new

See More: Delete 1st 4 lines & last 8 Lines in TXT File

Report •

July 13, 2009 at 05:58:00
This should work, BUT [here comes the but part] if there are blank lines it will take a bit more doing.

@echo off > newfile & setLocal enableDELAYedexpansion

set T= & set N=

for /f "tokens=* delims= " %%a in (myfile) do (
set /a T+=1

set /a E=!T!-4

for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
if !N! gtr 4 (
if !N! lss !E! (
echo.%%a >> newfile

If at first you don't succeed, you're about average.


Report •
Related Solutions

Ask Question