Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Dec 2009
    Location
    Palm Coast, Florida
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VB 2010: working with text files

    Since moving to Win 7 64bit I have an app that will not run so I need to write a program to replace it. I've worked with VBA in Excel in the past but I need a stand alone app so I am attempting to cobble together an app and I need some advice. The textfile that I have is a Unix generated file. I have been able to manipulate the file to get it into the following format.

    ADD-SCSU:1175,,0,0,30,30,0,3862544387,NAT,000000000000 ,N,,PHANTOM,20,20,N,,Y;
    ADD-SCSU:1211,1-2-109-9,0,0,43,43,0,3862544024,NAT,000000000000,N,,RPHON E,RP624SL,1,,10,,,20,20,ON,N,N,NONE,,Y,N;
    ADD-SCSU:1212,1-5-49-13,0,0,25,25,0,3862544147,UNDEFINE,000000000000,N, ,ANATE,,,DTMF,Y,1,,10,,,15,15,N,N,N;
    ADD-SCSU:1214,,0,0,31,31,0,3862544000,UNDEFINE,0000000 00000,N,,PHANTOM,31,31,N,,Y;
    ADD-SCSU:1233,1-7-25-2,0,0,25,25,0,3862544200,UNDEFINE,000000000000,N,, ANATE,,,DTMF,Y,,,10,,,20,20,N,N,N;
    ADD-SCSU:1236,1-6-97-3,0,0,40,40,0,3862544198,NAT,000000000000,N,,RPHON E,RP240,1,,10,,,20,20,ON,N,N,NONE,,Y,N;
    ADD-SCSU:1237,1-3-115-0,0,0,35,35,0,3862544135,UNDEFINE,000000007215,N,, RPHONE,RP120,1,1,10,,,15,15,OFF,N,N,NONE,,N,N;

    What I am trying to do is to create a textfile that contains only the strings that have the word "PHANTOM", a file for only the string "RPHONE", and a file for "ANATE". The difficulty that I am having , besides not having much vb programming knowledge, is not knowing how to get from the file that I have to the files that I want to create. I've spent several hours Googling the subject but I have no idea the best way to proceed and any advise will be appreciated. I am not asking for fully written code, although tidbits would be helpful as examples, I want to learn to program this myself. Thank you in advance.

    fuji056

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    There are several ways to do this but I would probably do the job by duplicating the text file first to make three identical copies and then in each one, iterate through the paragraphs and delete each paragraph that doesn't have the keyword
    eg in Word which could be adapted to your VB
    Dim aPara as Paragraph
    For each aPara in ActiveDocument.Paragraphs
    If InStr(aPara.Range.text,"PHANTOM")=0 then
    aPara.Delete
    end if
    Next aPara

    An alternative method that doesn't need any coding is to import the text file into a Microsoft Access table and then create three queries to display the relevant lines. These queries can be exported back out to text files pretty easily. Looking at the comma separated data, this might be the best option because you probably want to do other analysis on the data and Access is going to be the best for that.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    New Lounger
    Join Date
    Dec 2009
    Location
    Palm Coast, Florida
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, Andrew. I'll try your first solution as I would like to keep it all contained as one operation since I may not be the only one converting the data.

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Personally, I'd go the other way - open the one file, parse the data, then write out the parsed data to the separate files.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    New Lounger
    Join Date
    Dec 2009
    Location
    Palm Coast, Florida
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you both for your suggestions. I spend a little time looking around and I found something at MSDN that got me thinking in another direction and here is the code for my solution:

    sub findPhantom()
    fso = CreateObject("Scripting.FileSystemObject")
    NewFile = fso.CreateTextFile("C:\phantom.txt")
    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\h ello2.txt")
    MyReader.TextFieldType = FileIO.FieldType.Delimited
    MyReader.SetDelimiters(";")
    Dim currentRow As String()
    While Not MyReader.EndOfData
    Try
    currentRow = MyReader.ReadFields()
    Dim currentField As String
    For Each currentField In currentRow
    If InStr(currentField, "PHANTOM") Then
    NewFile.Write((currentField) & vbCrLf)
    End If
    Next
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineExceptio n
    MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
    End Try
    End While
    End Using
    End Sub

Posting Permissions

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