I have over 400+ emails saved in a single text file. Each email
is separated with a dotted line (e.g, one hundred dash "-'
characters).
Rather than manually copying and pasting each email
into a separate file, is there a faster solution? Can I use
regular expressions for this? I'm looking to create a script
that will grab all the emails and save each email into its own
separate file.
I know how to use VBA regular expressions to match text
in a single line... but, I'm not sure how I can match a block
of text located between a pair of dotted lines as described
above. I'd appreciate any tips. Thank you!
Rob
<FWIW
For simplicity, I separate blocks of text with "<" for non-titled
content, and "[Title goes here]" for titled content so it parses as an
ini file section.
You can Split() the file into an array, using the dotted line as the
delimiter. This puts each email as a separate element in the array. You
can write each element to its own file, but you'll need a methodology
to determine its filename. That might be as simple as using info within
the email, but doable nevertheless!
Example code...
Option Explicit
Sub ParseEmailFile()
Dim n&, vFile, vTmp
Const sFileIn$ = "C:\MyEmailFile.txt" '//edit to suit
Const sPath$ = "C:\MyEmails\" '//edit to suit
'Load the file
vFile = Split(ReadTextFile(sFileIn), String(100, "-")) '//edit to
suit
For n = LBound(vFile) To UBound(vFile)
'Get the target filename from a specific line in each email,
'or implement a unique naming scheme.
vTmp = Split(vFile(n), vbCrLf)
WriteTextFile sPath & vTmp(0) '//edit to suit
Next 'n
End Sub
Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)
ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()
Sub WriteTextFile(TextOut$, Filename$, _
Optional AppendMode As Boolean = False)
' Reusable procedure that Writes/Overwrites or Appends
' large amounts of data to a Text file in one single step.
' **Does not create a blank line at the end of the file**
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile()
If AppendMode Then
Open Filename For Append As #iNum: Print #iNum, vbCrLf & TextOut;
Else
Open Filename For Output As #iNum: Print #iNum, TextOut;
End If
ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Sub 'WriteTextFile()
--
Garry
Free usenet access at
http://www.eternal-september.org
Classic
VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.
vb.general.discussion