Consulting

Results 1 to 5 of 5

Thread: Open text file and delete lines.

  1. #1
    VBAX Regular
    Joined
    May 2007
    Posts
    87
    Location

    Open text file and delete lines.

    I'm looking for a bit of code to open text files and delete about four or five different types of lines.

    These are trail files for ProE and I want to get rid of mouse movements automatically and comments that aren't needed:

    e.g. Comments:
    !%CIDatum planes will be displayed.
    (always start with a '!')

    e.g. Zooms:
    ~ Wheel `main_dlg_cur` `proe_win` \
    9 809 831 458752 0 1529 947 1920 1200 107781
    ('~ Wheel' starts two lines)

    I've tried similar to the replace command:


    [vba]
    Sub filemoder()
    Dim VFF As Long, trailwpath As String, trailoutput As String, trailbody As String, trailbodyO As String, vIStep As String
    trailwpath = "C:\Path\trailfile.txt"
    trailoutput = "C:\Path\trailfile2.txt"

    VFF = FreeFile
    Open trailwpath For Binary As #VFF
    trailbody = Space$(LOF(VFF))
    Get #VFF, , trailbody
    Close #VFF
    trailbodyO = Replace(trailbody, "!", "")
    Open trailoutput For Output As #VFF
    Print #VFF, trailbodyO
    Close #VFF

    End Sub


    [/vba]

    But with poor results. Mainly because it just takes out the "!" comment denoter. So the file then falls over

    I was thinking maybe read the file line by line and then only write to next file string if it didn't read the "!" or the "~ wheel", but I don't know how to do that.

    Any ideas?

    -AS
    Last edited by andysuth; 08-15-2007 at 05:49 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim iLastRow As Long

    Workbooks.OpenText Filename:=vNIStep , _
    Origin:=xlMSDOS, _
    StartRow:=1, _
    DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=False, _
    Tab:=False, _
    Semicolon:=False, _
    Comma:=False, _
    Space:=False, _
    Other:=False, _
    FieldInfo:=Array(1, 1), _
    TrailingMinusNumbers:=True
    With ActiveWorkbook.Worksheets(1)

    iLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    For i = iLastRow To 1 Step -1

    If Left$(.Cells(i, "A").Value, 1) = "~" Then

    .Rows(i).Resize(2).Delete
    ElseIf Left$(.Cells(i, "A").Value, 1) = "!" Then

    .Rows(i).Delete
    End If
    Next i

    End With

    ActiveWorkbook.Save
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    May 2007
    Posts
    87
    Location
    Quote Originally Posted by xld
    [vba]
    If Left$(.Cells(i, "A").Value, 1) = "~" Then

    .Rows(i).Resize(2).Delete
    ElseIf Left$(.Cells(i, "A").Value, 1) = "!" Then

    .Rows(i).Delete
    End If[/vba]
    Clever, so I load the file into Excel and use the native formatting commands.

    Love it.

    -AS

  4. #4
    VBAX Regular
    Joined
    May 2007
    Posts
    87
    Location
    I've tried the code, works great, but there were other commands with the start ~ (in fact, most commands!), so I've modified as below.

    Unfortunately it's crashing out and I can't catch where, I'll keep you posted on this, but not sure it's a go idea simply because of ProE.

    I suspect it trips because of the LButton Arm / Disarm command, so I'll try later, but thanks for the help, and I've posted the code below for any other ProE saddo's out there who want to rationalise the trail files.

    -AS

    [VBA]


    Sub Hunter()
    Dim VFF As Long, trailwpath As String, trailoutput As String, trailbody As String, trailbodyO As String, vIStep As String
    Dim iLastRow As Long


    trailwpath = "C:\ProE_files\iD2M\trailfile.txt"


    Workbooks.OpenText Filename:=trailwpath, _
    Origin:=xlMSDOS, _
    StartRow:=1, _
    DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=False, _
    Tab:=False, _
    Semicolon:=False, _
    Comma:=False, _
    Space:=False, _
    Other:=False, _
    FieldInfo:=Array(1, 1), _
    TrailingMinusNumbers:=True
    With ActiveWorkbook.Worksheets(1)

    iLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    For i = iLastRow To 1 Step -1

    If Left$(.Cells(i, "A").Value, 7) = "~ Wheel" Then
    .Rows(i).Resize(2).Delete
    ElseIf Left$(.Cells(i, "A").Value, 6) = "~ LBut" Then
    .Rows(i).Resize(2).Delete
    ElseIf Left$(.Cells(i, "A").Value, 1) = "!" Then
    .Rows(i).Delete
    ElseIf Left$(.Cells(i, "A").Value, 1) = "<" Then
    .Rows(i).Delete
    End If
    Next i

    End With

    ActiveCell.Range("a1").Select
    ActiveCell.Rows.Insert
    ActiveCell.Value = "!trail file version No. 1350"

    ActiveWorkbook.Save
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    Application.DisplayAlerts = True

    End Sub

    [/vba]

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Andy,

    what do you mean by the arm/disarm button? If it is just cases that need to be catered for thst should be simple.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •