Computing.Net > Forums > Programming > Delete 1st 4 lines & last 8 Lines in TXT File

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

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

Reply to Message Icon

Name: DealMaker
Date: July 13, 2009 at 02:28:50 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Hello,

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:


-----BEGIN PGP SIGNED MESSAGE-----
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,

Brett

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
)
)
::==



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: July 13, 2009 at 05:58:00 Pacific
Reply:

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.

M2


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


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

remove duplicate lines in txt file www.computing.net/answers/programming/remove-duplicate-lines-in-txt-file/19710.html

Batch to edit 1st line of txt file www.computing.net/answers/programming/batch-to-edit-1st-line-of-txt-file-/17985.html

Select First Line in text.txt www.computing.net/answers/programming/select-first-line-in-texttxt/10514.html