Releases » Stuff » Visual Studio 2010 Macros »
Saves sequentially numbered backup copies of the current document.

Notes:

Public Module TwoUtilities

    ' Wed 21 Apr 03:16:04 2010 -two
    ' Creates sequentially numbered backup copies of the current document.
    ' The filename is made up from the document name, a number, and a .bak extension.
    ' Then saves the regular document as with Ctrl+S.
    ' Bind suggestion: Alt+S
	' For updates visit http://twodev.at
    Sub SaveSequentialBackup()

        Dim startPoint As EnvDTE.EditPoint
        Dim endPoint As EnvDTE.EditPoint
        Dim text As String
        Dim baseDir As String
        Dim baseName As String
        Dim fileName As String
        Dim backupDir As String
        Dim lastLen As Long

        baseDir = System.IO.Path.GetDirectoryName(DTE.ActiveDocument.FullName)
        baseName = System.IO.Path.GetFileName(DTE.ActiveDocument.FullName)
        backupDir = baseDir + "\Backups"
        My.Computer.FileSystem.CreateDirectory(backupDir)

        ' Determine backup filename
        For i = 1 To 999 Step 1
            fileName = backupDir + "\" + baseName + "." + Format(i, "000") + ".bak"
            If Dir(fileName) = "" Then
                Exit For
            End If
            lastLen = FileLen(fileName)
        Next

        ' Get document content
        If (DTE.ActiveDocument Is Nothing) Then
            Return
        End If
        startPoint = DTE.ActiveDocument.Object.StartPoint.CreateEditPoint
        endPoint = DTE.ActiveDocument.Object.EndPoint.CreateEditPoint
        text = startPoint.GetText(endPoint)

        ' Create temp document, save, then close,
        ' but only if text length is different than last backup file size
        If (text.Length() <> lastLen) Then

            DTE.ItemOperations.NewFile("General\Text File")
            DTE.ActiveDocument.Object("TextDocument").Selection.Insert(text)
            DTE.ActiveDocument.Save(fileName)
            DTE.ActiveDocument.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo)
            'MsgBox("Backup created: " + System.IO.Path.GetFileName(fileName))
        Else
            'MsgBox("No backup required.")
        End If

        ' Save main document
        DTE.ActiveDocument.Save()

    End Sub

End Module
 

Downloads:

No downloads available at this time.