Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 Bodyas 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 EnableDelayedExpansionif exist *.cln del *.cln
for /f "tokens=* delims= " %%A in ('dir /b *.txt') do (
set NAME=%%~nA
set fileNAME=%%Afind /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

:: edit email * dump first 3 lines
:: lesson learned: it's %%~Na NOT %%~Ne@echo off
setLocal EnableDelayedExpansionif 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

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.

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

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

@echo off > newfile
setLocal EnableDelayedExpansionfor /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

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

Heres my final script Which works
@echo off
setLocal EnableDelayedExpansionfor %%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
)
)

![]() |
![]() |
![]() |

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