views:

24

answers:

1

Here's my current Macro

Public Module CopyrightCode
    Sub AddCopyrightHeader()

        Dim doc As Document
        Dim docName As String
        Dim companyName As String = "Urban Now"
        Dim authorName As String = "Chase Florell"
        Dim authorEmail As String = "[email protected]"
        Dim copyrightText As String = "'     All code is Copyright © " & vbCrLf & _
        "'      -   Urban Now (http://urbannow.com)" & vbCrLf & _
        "'      -   Infinitas Advantage (http://infinitas.ws)" & vbCrLf & _
        "'     All Rights Reserved"

        ' Get the name of this object from the file name
        doc = DTE.ActiveDocument

        ' Get the name of the current document
        docName = doc.Name

        ' Set selection to top of document
        DTE.ActiveDocument.Selection.StartOfDocument()
        DTE.ActiveDocument.Selection.NewLine()

        Dim sb As New StringBuilder
        sb.Append("' --------------------------------")
        sb.Append(vbCrLf)
        sb.Append("' <copyright file='" & docName & "' company='" & companyName & "'>")
        sb.Append(vbCrLf)
        sb.Append(copyrightText)
        sb.Append(vbCrLf)
        sb.Append("' </copyright>")
        sb.Append(vbCrLf)
        sb.Append("' <author>" & authorName & "</author>")
        sb.Append(vbCrLf)
        sb.Append("' <email>" & authorEmail & "</email>")
        sb.Append(vbCrLf)
        sb.Append("' <lastedit>" & FormatDateTime(Date.Now, vbLongDate) & "</lastedit>")
        sb.Append(vbCrLf)
        sb.Append("' ---------------------------------")

        ' Write first line
        DTE.ActiveDocument.Selection.LineUp()
        DTE.ActiveDocument.Selection.Text = sb.ToString

    End Sub
End Module

What I need to do is first do a file search for the line ' <lastedit>Monday, July 05, 2010</lastedit> (obviously as a REGEX because the date will always be different)

and if it exists, replace the date with today's date, and if it doesn't run the full insert.

Then what I want to hook up is every time I close a file, the Macro runs to update the edit date.

+1  A: 

I'm not sure what you're doing, but if that is XML (as it looks like), you should be using XQuery or whatever to locate/update the lastedit node, since that'll handle the assorted complexities of comments and nesting and so on.

If you're confident of what the input text will be and are certain there's no nasties in there, you can match that specific date format quick and dirty:

<lastedit>\w{6,9}, \w{3,9} \d\d, \d{4}</lastedit>

Or, even quicker and dirtier:

<lastedit>[^<]+<lastedit>


It depends what your needs are, how confident you are of what the file contents will be, and so on.



Oh. So I was curious and went and looked up how Visual Studio actually does it's regex stuff, and well... whoever did the VS regex needs to be whacked round the head.

Translate the above standard regex into VS regex, you get these:

\<lastedit\>:i+, :i+ :d:d, :d:d:d:d\</lastedit\>

and

\<lastedit\>[^<]+</lastedit\>

Maybe. It's hard to read the documentation because Microsoft don't appear to want to write websites that work in modern browsers.

Of course, that assumes macros use this insane regex instead of the normal .NET regex - if it's the latter than the top stuff will be fine and you can ignore this craziness. :)


To implement, try something like this:

Dim reLastEdit As Regex = New Regex("<lastedit>[^<]+<lastedit>")

Dim matches AS MatchCollection = reLastEdit.Matches(Input)

If matches.Count > 0
Then
    ' Change Header
    Dim NewLastEdit As String = "<lastedit>" & FormatDateTime(Date.Now, vbLongDate) & "</lastedit>"
    reLastEdit.Replace(Input,NewLastEdit)
Else
    ' Add Header
EndIf

Or similar. Info here: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex_methods.aspx

Peter Boughton
What I'm doing is automatically adding a copyright header to all of my code files. BUT if the header is already there, we want to simply update the `lastedit` to the current date rather than insert the entire header again.
rockinthesixstring
It's not "actual" xml, it's just code comments.
rockinthesixstring
Thanks for that. I agree, the documentation is unreadable in FF. The Regex part I think I can handle, the challenge I'm facing is implementing it into a Macro. 1) read the 11th line of the file 2) if exists - update date - else - add header.
rockinthesixstring
I found something that suggested Macros use regular .NET stuff, so I've thrown something together - but I don't do VB so it's probably not exactly right. The examples and Methods section here may help further though: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx
Peter Boughton
thanks for looking into it... I'll get cracking and post my results. Thanks.
rockinthesixstring
beautiful... I got it working.
rockinthesixstring