pattern match processing of multiple .frm files

2019-03-07 02:42发布

问题:

I have multiple VB6 .frm files. See example below. I want to strip away the functions and subs from the code and leave only the form design.

What I need to do is find the last line starting with "Attribute" because after this line everything further should be deleted.

Using pattern matching or something similar, how can I process the .frm files so that everything after the last Attribute line is deleted? If I am traversing through a file, how can I tell where the last Attribute line is?

Example of .frm file:

VERSION 5.00
Begin VB.Form Form1
    Caption = "Form1"
    ClientHeight = 3195
    ClientLeft = 60
    ClientTop = 345
    ClientWidth = 4680
    LinkTopic = "Form1"
    ScaleHeight = 3195
    ScaleWidth = 4680
    StartUpPosition = 3 'Windows Default
    Begin VB.CommandButton Command1
        Caption = "Command1"
        Height = 495
        Left = 1800
        TabIndex = 1
        Top = 1320
        Width = 1215
    End
    Begin VB.TextBox Text1
        Height = 495
        Left = 360
        TabIndex = 0
        Text = "Text1"
        Top = 240
        Width = 1215
    End
End

Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private Sub Command1_Click()
    Text1.Text = "Hello World"
End Sub
Private Sub Form_Load()
    Text1.BackColor = vbBlue
End 

回答1:

You just need 2 rules:

1) If line starts with 'Attribute' then don't delete. 2) If line starts with 'Attribute' set a flag to start deleting all subsequent lines.

Rule #1 will prevent you from deleting subsequent Attribute lines, and there should be nothing you want to keep after the first Attribute you encounter unless it's an Attribute.