Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Extract lines with regular expressions?

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



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Extract lines with regular expressions?

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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Extract lines with regular expressions?

Oops! I forgot to include the email content...

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 vFile(n), sPath & vTmp(0) '//edit to suit

Next 'n
End Sub


--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Extract lines with regular expressions?

Thanks Garry. I will test this when I get home.

"GS" wrote:

Oops! I forgot to include the email content...

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 vFile(n), sPath & vTmp(0) '//edit to suit

Next 'n
End Sub




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expressions in VB FiluDlidu Excel Discussion (Misc queries) 4 March 21st 08 01:10 AM
Regular Expressions sl Excel Programming 2 January 22nd 07 11:27 PM
Using Regular Expressions with VBA Andrew Hall NZ Excel Programming 5 November 21st 06 09:30 PM
Batch extract PDF data to Excel using regular expressions [email protected] Excel Programming 0 July 25th 06 10:12 AM
VBA and Regular expressions Friedrich Muecke Excel Programming 3 October 3rd 03 01:46 AM


All times are GMT +1. The time now is 08:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"