Computing.Net > Forums > Programming > Batch to del first x lines of text

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.

Batch to del first x lines of text

Reply to Message Icon

Name: evileyeball
Date: August 26, 2008 at 14:37:22 Pacific
OS: XP
CPU/Ram: p42gb
Product: me
Comment:

What I am trying to do is delete the first X number of lines from a text file using a batch file on all text files in a folder.

all files are in the format
From: <Address>
To: <Addresses on individual lines>
Date: <Date>
Subject: <Subject>
---------------
Message Body

as they used to be e-mails

My Batch file should remove all the lines ABOVE subject so the E-mail Subject is the first line in the file.

What I have so far which somewhat works in XP is below It needs to run in xp but i don't care otherwise

::== CLeaning OF E-mails
@echo off
setLocal EnableDelayedExpansion

if exist *.cln del *.cln

for /f "tokens=* delims= " %%A in ('dir /b *.txt') do (

set NAME=%%~nA
set fileNAME=%%A

find /n "Subject:" !fileNAME! > out.dat
for /F "skip=2 eol= tokens=*" %%S in (out.dat) do (set str=%%S&goto:forend)
:forend
set str=!str:~1,2!
set /a str-= 1
del out.dat
echo %str%

set optn="tokens=* skip=!str! delims= "

for /f %optn% %%L in (!fileNAME!) do (echo %%L >> !NAME!.cln )

)
::==

That works on the first file in the folder but for some reason doesnt do any others.
Its been a long while since I did major batch programming but with the help of some examples and code on other postings here I have gotten this far Now I seek assistance




Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: August 26, 2008 at 21:59:41 Pacific
Reply:

:: edit email * dump first 3 lines
:: lesson learned: it's %%~Na NOT %%~Ne

@echo off
setLocal EnableDelayedExpansion

if exist *.new del *.new

for /f "tokens=* delims= " %%a in ('dir/b/a-d *.txt') do (
set N=
for /f "tokens=* delims= " %%e in (%%a) do (
set /a N+=1
if !N! gtr 3 echo %%e >> %%~Na.new
)
)


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

M2


0

Response Number 2
Name: Judago
Date: August 26, 2008 at 22:27:15 Pacific
Reply:

From what I gather you only need to skip the first 3 lines of text and you want the rest of the file in a file with the same name but the extension .cln. If this is correct the below seemed to work for me(and a 1 liner)

FOR %%G IN (*.TXT) DO FOR /F "SKIP=3 TOKENS=*" %%H IN (%%~sG) DO ECHO %%H>>%%~nG.cln

[edit]
Mechanix2Go your script doesn't work if the file names contain spaces!

[edit]
got mixed up about.cln and .new.


0

Response Number 3
Name: Mechanix2Go
Date: August 26, 2008 at 23:42:55 Pacific
Reply:

Hi Judago,

Good idea, using SKIP. And you're right about names with spaces.


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

M2


0

Response Number 4
Name: evileyeball
Date: August 26, 2008 at 23:49:33 Pacific
Reply:

No Not 3 lines X lines

Because Each file is like

From: Some@Email.com
To: Recipent1@Email.com
Recipient2@Blan.com
Recipient3@IamALine.com
Recipient4@Oh.com
...
...
...
RecipientX@IMightBeManyLinesDown.com
Date: 08/08/2008
Subject: Hey Recipients
---------------
I'm Testing my E-mail Its so cool to chat wit h you all


See what i mean


0

Response Number 5
Name: Judago
Date: August 27, 2008 at 00:09:41 Pacific
Reply:

okay so it's x lines, does the first line you want always start with "subject"?


0

Related Posts

See More



Response Number 6
Name: evileyeball
Date: August 27, 2008 at 00:41:19 Pacific
Reply:

Yes the first line will allways begin with Subject:


0

Response Number 7
Name: Mechanix2Go
Date: August 27, 2008 at 01:17:14 Pacific
Reply:

@echo off > newfile
setLocal EnableDelayedExpansion

for /f "tokens=1* delims= " %%a in (email.txt) do (
echo %%a | find "Subject:" > nul
if not errorlevel 1 set keep=y
if !keep!'==y' echo %%a %%b >> newfile
)


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

M2


0

Response Number 8
Name: evileyeball
Date: August 27, 2008 at 01:31:54 Pacific
Reply:

Works, but Needs to do all text files in a folder...


0

Response Number 9
Name: evileyeball
Date: August 27, 2008 at 01:41:22 Pacific
Reply:

Never mind, I fixed that problem myself, You guys are a godsend...

Now that it works on my test files, I cna put it into work on my real files


0

Response Number 10
Name: evileyeball
Date: August 27, 2008 at 01:42:28 Pacific
Reply:

Heres my final script Which works

@echo off
setLocal EnableDelayedExpansion

for %%I in (*.txt) DO (
set keep=
for /f "tokens=1* delims= " %%a in (%%I) do (
echo %%a | find "Subject:" > nul
if not errorlevel 1 set keep=y
if !keep!'==y' echo %%a %%b >> %%I.cln
)
)



0

Sponsored Link
Ads by Google
Reply to Message Icon

C Program: Hex value outp... Batch that grabs a random...



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: Batch to del first x lines of text

Delete first line of text www.computing.net/answers/programming/delete-first-line-of-text/13312.html

Batch file to extract lines of text from .ini www.computing.net/answers/programming/batch-file-to-extract-lines-of-text-from-ini/19089.html

.bat delete first line of text file www.computing.net/answers/programming/bat-delete-first-line-of-text-file/13773.html