Results 1 to 35 of 35

Thread: Using visual basic + windows form to remove unnecassary lines in a text file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Question Using visual basic + windows form to remove unnecassary lines in a text file

    Hi everyone,

    First post so apologies if it breaks any rules.

    I am trying to edit a file to remove unnecessary information. The file usually gets sent to a machining centre, however this particular item gets routed on a manual router. It needs to be trimmed down and printed for the shop floor.

    Example;

    Changes the text contents from,

    B,00015501,S
    L,5HV71 ,RW ,1,60000,001
    P,19180,||,01,17,P202503/008 1 Extende Top ,0900,0900,000000000001
    O,1520,NONOPT,000,000,000
    O,6300,NONOPT,000,000,000
    O,9550,NONOPT,000,000,000
    O,17570,NONOPT,000,000,000
    P,14650,||,01,15,P202503/006 1 Extende Top ,0900,0900,000000000002
    O,4210,NONOPT,000,000,000
    O,11380,NONOPT,000,000,000
    P,09100,||,01,23,P202529/005 1 Extende Top ,0900,0900,000000000003
    O,2250,NONOPT,000,000,000
    O,6750,NONOPT,000,000,000
    P,09050,||,01,14,P202503/005 1 Extende Top ,0900,0900,000000000004
    O,2250,NONOPT,000,000,000
    O,6750,NONOPT,000,000,000
    P,06750,||,01,20,P202529/002 1 Extende Top ,0900,0900,000000000005
    O,3370,NONOPT,000,000,000
    R,00570,||,W,000000000001
    L,5HV71 ,RW ,1,60000,001
    P,19180,||,01,08,P202496/008 1 Extende Top ,0900,0900,000000000006
    O,1520,NONOPT,000,000,000
    O,6300,NONOPT,000,000,000
    O,9550,NONOPT,000,000,000
    O,17570,NONOPT,000,000,000
    P,19150,||,01,26,P202529/008 1 Extende Top ,0900,0900,000000000007
    O,1520,NONOPT,000,000,000
    O,6300,NONOPT,000,000,000
    O,9550,NONOPT,000,000,000
    O,17570,NONOPT,000,000,000
    P,11350,||,01,04,P202496/004 1 Extende Top ,0900,0900,000000000008
    O,3260,NONOPT,000,000,000
    O,8790,NONOPT,000,000,000
    P,08950,||,01,05,P202496/005 1 Extende Top ,0900,0900,000000000009
    O,2250,NONOPT,000,000,000
    O,6750,NONOPT,000,000,000
    R,00770,||,W,000000000002

    to,

    00015501,
    P,19180,P202503/008 1 Extende Top ,
    1520,NONOPT,
    6300,NONOPT,
    9550,NONOPT,
    17570,NONOPT,
    P,14650,P202503/006 1 Extende Top ,
    4210,NONOPT
    11380,NONOPT
    P,09100,P202529/005 1 Extende Top ,
    2250,NONOPT,
    6750,NONOPT,
    P,09050,P202503/005 1 Extende Top ,
    2250,NONOPT,
    6750,NONOPT,
    P,06750,P202529/002 1 Extende Top ,
    O,3370,NONOPT,
    P,19180,P202496/008 1 Extende Top ,
    1520,NONOPT,
    6300,NONOPT,
    9550,NONOPT,
    17570,NONOPT,
    P,19150,P202529/008 1 Extende Top ,
    1520,NONOPT,
    6300,NONOPT,
    9550,NONOPT,
    17570,NONOPT,
    P,11350,P202496/004 1 Extende Top ,
    3260,NONOPT,
    8790,NONOPT,
    P,08950,P202496/005 1 Extende Top ,
    2250,NONOPT,
    6750,NONOPT,

    Explanation as to what it is;

    B,00015501,S Batch Number

    L,5HV71 ,RW ,1,60000,001 Item/Colour/Length

    P,19180,||,01,17,P202503/008 1 Extende Top ,0900,0900,000000000001Size/Job/Position

    O,1520,NONOPT,000,000,000Routing 1
    O,6300,NONOPT,000,000,000Routing 2
    O,9550,NONOPT,000,000,000Routing 3
    O,17570,NONOPT,000,000,000Routing 4

    Piece 2
    P,14650,||,01,15,P202503/006 1 Extende Top ,0900,0900,000000000002
    O,4210,NONOPT,000,000,000
    O,11380,NONOPT,000,000,000

    Piece 3
    P,09100,||,01,23,P202529/005 1 Extende Top ,0900,0900,000000000003
    O,2250,NONOPT,000,000,000
    O,6750,NONOPT,000,000,000

    Piece 4
    P,09050,||,01,14,P202503/005 1 Extende Top ,0900,0900,000000000004
    O,2250,NONOPT,000,000,000
    O,6750,NONOPT,000,000,000

    Piece 5
    P,06750,||,01,20,P202529/002 1 Extende Top ,0900,0900,000000000005
    O,3370,NONOPT,000,000,000

    Offcut from bar length
    R,00570,||,W,000000000001

    Repeat for each bar length

    This is as far as I have come,

    Code:
    Option Strict On
    Imports System.IO
    Imports System.Environment
    Public Class HVRoutForm
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSelectBatch.Click
    OpenF.InitialDirectory = "C:\Users\fradleyp\Desktop\Work script\"
    OpenF.FileName = "Select A Batch.."
    OpenF.Filter = "Only .449 Files (*.449) |*.449"
    OpenF.ShowDialog()
    Try
    Dim sr As New StreamReader(OpenF.FileName)
    Dim lineJustReadFromTheFile As String
    Dim anArray(0) As String
    Dim myList As New List(Of String)
    While sr.EndOfStream = False
    lineJustReadFromTheFile = sr.ReadLine
    anArray = lineJustReadFromTheFile.Split(Convert.ToChar(","))
    myList.AddRange(anArray)
    End While
    sr.Close()
    Dim itemsArray() As String = myList.ToArray
    Dim outPutString As String = ""
    For index As Integer = 0 To itemsArray.GetUpperBound(0)
    outPutString &= itemsArray(index) & NewLine
    Next
    MessageBox.Show(outPutString)
    Catch
    End Try
    End Sub
    End Class
    At the moment it just breaks down the file and then outputs it. I don't know how to remove lines from it.

    Any help would be gratefully received,

    Thanks in advance
    Last edited by fradleyp; Jul 19th, 2014 at 03:49 PM.

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    I believe this is a simple matter of taking portions of the string. There is a method called substring that you can use. You should research this method. The indexes of the substring you will pull appears to be determined by the leading character such as "B" or "O". I also would check for the comma appearing after the initial character so that you have a better chance of not parsing an incorrect line if it ever occured (the comma makes it a bit more unique than just a letter to check against). Note this works for your example, and as long as you expect the file to appear the same length for each line.


    Code:
     Using sr As New IO.StreamReader("inputpath")
                Using sw As New IO.StreamWriter("outputpath") 'or use a list or something similar
    
                    While sr.Peek() >= 0
    
                        Dim lineRead As String
                        lineRead = sr.ReadLine()
    
                        Select Case lineRead.Substring(0, 2) '
                            Case "L,", "R,"
                                Continue While
                            Case "B,"
                                lineRead = lineRead.Substring(2, 9)
                            Case "P,"
                                lineRead = lineRead.Substring(0, 8) & lineRead.Substring(17, 27)
                            Case "O,"
                                lineRead = lineRead.Substring(2, 12)
                        End Select
    
                        sw.WriteLine(lineRead)
    
                    End While
                End Using
            End Using

    Also, a list is basically an array wrapped with methods that make it easier to deal with. Converting a list to array is kind of pointless unless you have very specific needs. You can achieve the same results doing:

    Code:
    for each item in mylist
    'do code
    next item
    or

    Code:
    for i = 0 to mylist.count - 1
    'do code
    next i
    No need to get upper boundary.
    Last edited by jayinthe813; Jul 14th, 2014 at 08:44 AM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Sorry I am very inexperienced, what would I need to do with my code to remove the following each time.

    B
    S
    L
    5HV71
    RW
    1
    60000
    001
    ||
    01
    17
    0900
    000000000001
    O
    000

    Then overwrite the original file or create a new one?

  4. #4
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by fradleyp View Post
    Sorry I am very inexperienced, what would I need to do with my code to remove the following each time.

    B
    S
    L
    5HV71
    RW
    1
    60000
    001
    ||
    01
    17
    0900
    000000000001
    O
    000

    Then overwrite the original file or create a new one?
    You may be inexperienced, but you can still search around online. You can use the replace method on strings if you need to:

    Code:
            Dim mystr As String = "My favorite part is part# 5HV71"
            'remove the instance of "5HV71
            mystr = mystr.Replace("5HV71", String.Empty) 'or replace String.Empty with ""
    You most likely cannot use this method with your file, however, because replacing "O" in the string will change all "O" that occur in the string with whatever you told it to replace with. In your case, if the file is output the same, you may be better off to test the line, then remove by position.

  5. #5
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    fradleyp:

    One thing that causes a little confusion is that some of your output lines in your initial sample
    end in commas while others do not. I assume this is a typing error on your part.
    Last edited by Gruff; Jul 16th, 2014 at 02:28 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by Gruff View Post
    fradleyp:

    One thing that causes a little confusion is that some of your output lines in your initial sample
    do not end in commas while others do not. I assume this is a typing error on your part.
    The initial is a direct output from the file.

    Apologies however it does look like I have messed up what it should look like and missed commas from the second piece.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Still struggling with this, what would I need to add to my code to do the following;

    Instead of separating the file by commas and outputting on screen. I want it to delete the following from the file;
    5HV71
    RW

    And print the results to the default printer?

  8. #8
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by fradleyp View Post
    Still struggling with this, what would I need to add to my code to do the following;

    Instead of separating the file by commas and outputting on screen. I want it to delete the following from the file;
    5HV71
    RW

    And print the results to the default printer?

    Did you not see the string.replace method above?

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Yes, however I don't fully understand how it works as the information isn't always in the same place.

    B,00015501,S
    L,5HV71 ,RW ,1,60000,001
    P,19180,||,01,17,P202503/008 1 Extende Top ,0900,0900,000000000001
    O,1520,NONOPT,000,000,000
    O,6300,NONOPT,000,000,000
    O,9550,NONOPT,000,000,000
    O,17570,NONOPT,000,000,000
    P,14650,||,01,15,P202503/006 1 Extende Top ,0900,0900,000000000002
    O,4210,NONOPT,000,000,000
    O,11380,NONOPT,000,000,000

    The first section has 4 operations, the second only as two. I could have up to 6 in each section meaning the positions always change.

    B,00015501,S
    P,19180,P202503/008 1 Extende Top ,
    1520,NONOPT,
    6300,NONOPT,
    9550,NONOPT,
    17570,NONOPT,
    P,14650,P202503/006 1 Extende Top ,
    4210,NONOPT,
    11380,NONOPT,

  10. #10
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    I am not entirely sure as your explanation to me is unclear. The first thing you should do is read the first character from the line this determines what your action is and how to output the line. Did you try my code above? What did that code do that you didnt like or expect?

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Thumbs up Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Thank you for your patience it's very much appreciated.

    I have removed my unwanted information with mylist.remove, I've probably quadrupled my coding however it works.

    From the this;

    B,00015601,S
    L,5HV71 ,RW ,1,60000,001
    P,19180,||,01,17,P202503/008 1 Extende Top ,0900,0900,000000000001
    O,2340,NONOPT,000,000,000
    O,7270,NONOPT,000,000,000
    O,11770,NONOPT,000,000,000
    O,16690,NONOPT,000,000,000
    P,14650,||,01,15,P202503/006 1 Extende Top ,0900,0900,000000000002
    O,4210,NONOPT,000,000,000
    O,11380,NONOPT,000,000,000
    P,09100,||,01,23,P202529/005 1 Extende Top ,0900,0900,000000000003
    O,2360,NONOPT,000,000,000
    O,6690,NONOPT,000,000,000
    P,09050,||,01,14,P202503/005 1 Extende Top ,0900,0900,000000000004
    O,2350,NONOPT,000,000,000
    O,6700,NONOPT,000,000,000
    P,06750,||,01,20,P202529/002 1 Extende Top ,0900,0900,000000000005
    O,3370,NONOPT,000,000,000
    R,00570,||,W,000000000001

    I am now outputting on screen the following;

    00015601
    19180
    P202503/008 1 Extende Top
    2340
    NONOPT
    7270
    NONOPT
    11770
    NONOPT
    16690
    NONOPT
    14650
    P202503/006 1 Extende Top
    4210
    NONOPT
    11380
    NONOPT
    09100
    P202529/005 1 Extende Top
    2360
    NONOPT
    6690
    NONOPT
    09050
    P202503/005 1 Extende Top
    2350
    NONOPT
    6700
    NONOPT
    06750
    P202529/002 1 Extende Top
    3370
    NONOPT
    00570

    Which is almost perfect, however I still want to remove the items in red which appear in the last line before the start of a new bar length as it is the wastage for that bar. Below is an example of two bar length being used,

    00015601
    19180
    P202503/008 1 Extende Top
    2340
    NONOPT
    7270
    NONOPT
    11770
    NONOPT
    16690
    NONOPT
    14650
    P202503/006 1 Extende Top
    4210
    NONOPT
    11380
    NONOPT
    09100
    P202529/005 1 Extende Top
    2360
    NONOPT
    6690
    NONOPT
    09050
    P202503/005 1 Extende Top
    2350
    NONOPT
    6700
    NONOPT
    06750
    P202529/002 1 Extende Top
    3370
    NONOPT
    00570
    19180
    P202503/008 1 Extende Top
    2340
    NONOPT
    7270
    NONOPT
    11770
    NONOPT
    16690
    NONOPT
    14650
    P202503/006 1 Extende Top
    4210
    NONOPT
    11380
    NONOPT
    09100
    P202529/005 1 Extende Top
    2360
    NONOPT
    6690
    NONOPT
    09050
    P202503/005 1 Extende Top
    2350
    NONOPT
    6700
    NONOPT
    06750
    P202529/002 1 Extende Top
    3370
    NONOPT
    00570



    Also instead of outputting to the screen I want it to output to the default printer if that is possible?

    For your information my code is now;

    Code:
    Option Strict On
    Imports System.IO
    Imports System.Environment
    Public Class HVRoutForm
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSelectBatch.Click
            OpenF.InitialDirectory = "C:\Users\fradleyp\Desktop\Work script\"
            OpenF.FileName = "Select A Batch.."
            OpenF.Filter = "Only .449 Files (*.449) |*.449"
            OpenF.ShowDialog()
            Try
                Dim sr As New StreamReader(OpenF.FileName)
                Dim lineJustReadFromTheFile As String
                Dim anArray(0) As String
                Dim myList As New List(Of String)
                While sr.EndOfStream = False
                    lineJustReadFromTheFile = sr.ReadLine
                    anArray = lineJustReadFromTheFile.Split(Convert.ToChar(","))
                    myList.AddRange(anArray)
                    For a = 11 To 30
                        myList.Remove("L")
                        myList.Remove("B")
                        myList.Remove("000")
                        myList.Remove("5HV71     ")
                        myList.Remove("RW   ")
                        myList.Remove("1")
                        myList.Remove("60000")
                        myList.Remove("001")
                        myList.Remove("P")
                        myList.Remove("S")
                        myList.Remove("R")
                        myList.Remove("W")
                        myList.Remove("||")
                        myList.Remove("01")
                        myList.Remove("02")
                        myList.Remove("03")
                        myList.Remove("04")
                        myList.Remove("05")
                        myList.Remove("06")
                        myList.Remove("07")
                        myList.Remove("08")
                        myList.Remove("09")
                        myList.Remove("10")
                        myList.Remove("11")
                        myList.Remove("12")
                        myList.Remove("13")
                        myList.Remove("14")
                        myList.Remove("15")
                        myList.Remove("16")
                        myList.Remove("17")
                        myList.Remove("18")
                        myList.Remove("19")
                        myList.Remove("20")
                        myList.Remove("21")
                        myList.Remove("22")
                        myList.Remove("23")
                        myList.Remove("24")
                        myList.Remove("25")
                        myList.Remove("26")
                        myList.Remove("27")
                        myList.Remove("28")
                        myList.Remove("29")
                        myList.Remove("30")
                        myList.Remove("31")
                        myList.Remove("32")
                        myList.Remove("33")
                        myList.Remove("34")
                        myList.Remove("35")
                        myList.Remove("36")
                        myList.Remove("37")
                        myList.Remove("38")
                        myList.Remove("39")
                        myList.Remove("40")
                        myList.Remove("0900")
                        myList.Remove("000000000001")
                        myList.Remove("000000000002")
                        myList.Remove("000000000003")
                        myList.Remove("000000000004")
                        myList.Remove("000000000005")
                        myList.Remove("000000000006")
                        myList.Remove("000000000007")
                        myList.Remove("000000000008")
                        myList.Remove("000000000009")
                        myList.Remove("0000000000010")
                        myList.Remove("0000000000011")
                        myList.Remove("0000000000012")
                        myList.Remove("0000000000013")
                        myList.Remove("0000000000014")
                        myList.Remove("0000000000015")
                        myList.Remove("0000000000016")
                        myList.Remove("0000000000017")
                        myList.Remove("0000000000018")
                        myList.Remove("0000000000019")
                        myList.Remove("0000000000020")
                        myList.Remove("0000000000021")
                        myList.Remove("0000000000022")
                        myList.Remove("0000000000023")
                        myList.Remove("0000000000024")
                        myList.Remove("0000000000025")
                        myList.Remove("0000000000026")
                        myList.Remove("0000000000027")
                        myList.Remove("0000000000028")
                        myList.Remove("0000000000029")
                        myList.Remove("0000000000030")
                        myList.Remove("0000000000031")
                        myList.Remove("0000000000032")
                        myList.Remove("0000000000033")
                        myList.Remove("0000000000034")
                        myList.Remove("0000000000035")
                        myList.Remove("0000000000036")
                        myList.Remove("0000000000037")
                        myList.Remove("0000000000038")
                        myList.Remove("0000000000039")
                        myList.Remove("0000000000040")
                        myList.Remove("O")
                    Next
                End While
                sr.Close()
                Dim itemsArray() As String = myList.ToArray
                Dim outPutString As String = ""
                For index As Integer = 0 To itemsArray.GetUpperBound(0)
                    outPutString &= itemsArray(index) & NewLine
                Next
                MessageBox.Show(outPutString)
            Catch
            End Try
        End Sub
    End Class
    Last edited by fradleyp; Jul 19th, 2014 at 03:48 PM.

  12. #12
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Please please please use code or highlight tags when posting code.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by ident View Post
    Please please please use code or highlight tags when posting code.
    Code all nicely wrapped in code quotes now, any chance you could help me with the problem?

  14. #14
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    As jayinthe813 said, you could just look at the first letter to decide what to do.
    In your case, see if you add this If block around your processing does the Red line numbers go away.
    (i.e. If the line starts with "R," just skip it.)
    Code:
                While sr.EndOfStream = False
                    lineJustReadFromTheFile = sr.ReadLine
                    If lineJustReadFromTheFile.Substring(0, 2) <> "R," Then
                        anArray = lineJustReadFromTheFile.Split(Convert.ToChar(","))
                        myList.AddRange(anArray)
                        For a = 11 To 30
                            myList.Remove("L")
                            myList.Remove("B")
                            myList.Remove("000")
                         '.... etc, etc
                           myList.Remove("0000000000040")
                           myList.Remove("O")
                        Next
                    End If
                End While

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by passel View Post
    As jayinthe813 said, you could just look at the first letter to decide what to do.
    In your case, see if you add this If block around your processing does the Red line numbers go away.
    (i.e. If the line starts with "R," just skip it.)
    Code:
                While sr.EndOfStream = False
                    lineJustReadFromTheFile = sr.ReadLine
                    If lineJustReadFromTheFile.Substring(0, 2) <> "R," Then
                        anArray = lineJustReadFromTheFile.Split(Convert.ToChar(","))
                        myList.AddRange(anArray)
                        For a = 11 To 30
                            myList.Remove("L")
                            myList.Remove("B")
                            myList.Remove("000")
                         '.... etc, etc
                           myList.Remove("0000000000040")
                           myList.Remove("O")
                        Next
                    End If
                End While
    Wow so perfect and yet so simple, thank you very very much.

    I have two last questions to make it absolutely 100% pefect.

    1.

    Currently the output on screen goes;
    00015601
    19180
    P202503/008 1 Extende Top
    2340
    NONOPT
    7270
    NONOPT
    1170
    NONOPT
    etc
    etc
    etc

    How can I rebuild it so that it follows the original pattern?
    00015601
    19180, P202503/008 1 Extende Top
    2340,NONOPT
    7270,NONOPT
    1170,NONOPT
    etc
    etc
    etc

    2. And the very last thing (I promise) Is can I print to the default printer instead of displaying on screen?

    Thanks once again for all your help getting me this far.

  16. #16
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by fradleyp View Post
    Wow so perfect and yet so simple, thank you very very much.

    I have two last questions to make it absolutely 100% pefect.

    1.

    Currently the output on screen goes;
    00015601
    19180
    P202503/008 1 Extende Top
    2340
    NONOPT
    7270
    NONOPT
    1170
    NONOPT
    etc
    etc
    etc

    How can I rebuild it so that it follows the original pattern?
    00015601
    19180, P202503/008 1 Extende Top
    2340,NONOPT
    7270,NONOPT
    1170,NONOPT
    etc
    etc
    etc

    2. And the very last thing (I promise) Is can I print to the default printer instead of displaying on screen?

    Thanks once again for all your help getting me this far.
    #1 - Just store the original text before you perform your specific modifications. You can always derive your modified version from it, so its pointless to convert "back and forth". If you absolutely need to restore the original, you will need to know what you modified and where you modified. This is difficult. Essentially you would create a "history" of changes and some sort of undo. Any time you want to convert back, just display the original, and then any time you want to show modified, parse the original. Keep it simple. All you need to store is the original.

    #2) Since you dont bother reading or trying my code examples above, i'm not going to waste my time writing code. You can read up on the PrintDocument class:

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

  17. #17
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    There is no pattern to follow. I am confused to hell. So if any line contains

    Code:
    B
    S
    L
    5HV71
    RW
    1
    60000
    001
    ||
    01
    17
    0900
    000000000001
    O
    000
    it should be removed?

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by jayinthe813 View Post
    #1 - Just store the original text before you perform your specific modifications. You can always derive your modified version from it, so its pointless to convert "back and forth". If you absolutely need to restore the original, you will need to know what you modified and where you modified. This is difficult. Essentially you would create a "history" of changes and some sort of undo. Any time you want to convert back, just display the original, and then any time you want to show modified, parse the original. Keep it simple. All you need to store is the original.

    #2) Since you dont bother reading or trying my code examples above, i'm not going to waste my time writing code. You can read up on the PrintDocument class:

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx
    I'm really sorry Jay, until today I only registered your first post which I tried and had no luck due to the randomness of it all. It's only after I tried the myList.remove did I read your second post which would have also worked perfectly if not more so.

    As you can probably tell before my first post I had no idea how to even start coding. I'd like to thank you for your time and effort and although I don't understand your point 1, I will try again at the printing part. I have researched this part for several hours, however I don't understand where each part fits in, although I will keep trying.

    Thanks once again for your time.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by ident View Post
    There is no pattern to follow. I am confused to hell. So if any line contains

    Code:
    B
    S
    L
    5HV71
    RW
    1
    60000
    001
    ||
    01
    17
    0900
    000000000001
    O
    000
    it should be removed?

    I have everything removed I need now, thank you.

  20. #20
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Yes but your code is bad. I suggest you answer my questions.

  21. #21
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by fradleyp View Post
    I'm really sorry Jay, until today I only registered your first post which I tried and had no luck due to the randomness of it all. It's only after I tried the myList.remove did I read your second post which would have also worked perfectly if not more so.

    As you can probably tell before my first post I had no idea how to even start coding. I'd like to thank you for your time and effort and although I don't understand your point 1, I will try again at the printing part. I have researched this part for several hours, however I don't understand where each part fits in, although I will keep trying.

    Thanks once again for your time.
    Ok so, not to sound too confusing:

    1) In this program you are essentially taking an input and producing an output. The input is referring to the strings pulled directly from the file. The output is what you see after you have finished running your coding to remove the elements. You are saying now you want to take the output, and convert it back to the original input. What I am saying is why convert it back, when you can just show the original input. In fact, you can always derive your output, so the input is all you ever need to store. Does this make sense? You can always create the output strings as long as you have the input strings. The input strings here are the key. Store those, and you can display this data however you want.

  22. #22
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Since you want several "fields" from a given line to be present on the output line in the same order, I just assumed you would have expanded on jayinthe813's original case statement, and parse each line based on the starting character and what portions of the line you want.
    My approach would be to do the parsing for one line, i.e. "B," until you got that one correct, then add a case for the next case "P, ", and after that, then next etc...
    It seems like from your first post, you would only have one more case to do, "O," after B and P were done, since any other case you would just skip.
    Here is my take on doing the first two cases, "B," and "P,".
    If you step through and understand how the "B," and "P," cases are selecting (and combining multiple substrings in "P,"'s case), you should be able to add the "O," case pretty quickly.
    Code:
    Public Class Form1
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim lines() As String
        Dim sIdx, eIdx As Integer
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
    
        lines = System.IO.File.ReadAllLines("c:\c\parse_in.txt")
        For i As Integer = 0 To lines.Count - 1
          Select Case lines(i).Substring(0, 2)
            Case "B,"
              sIdx = lines(i).IndexOf(",") + 1                          'Find first character after the first ","
              eIdx = lines(i).IndexOf(",", sIdx) + 1                    'Find next "," after that, and include it
              ListBox1.Items.Add(lines(i).Substring(sIdx, eIdx - sIdx)) 'Add that substring to the list
    
            Case "P,"
              sb.Clear()
              eIdx = lines(i).IndexOf(",", 2)                  'find the next "," starting from the third character (2)
              sb.Append(lines(i).Substring(0, eIdx))           'Append that to the stringbuilder obj, without the trailing ","
              sIdx = lines(i).IndexOf(",P")                    'Find where the ",P" starts (pick up the leading ",")
              eIdx = lines(i).IndexOf(",", sIdx + 1) + 1       'Find the next "," after that, and include it (+ 1)
              sb.Append(lines(i).Substring(sIdx, eIdx - sIdx)) 'Append it to the stringbuilder obj
              ListBox1.Items.Add(sb.ToString)                  'Convert the stringbuilder obj to a string and add it to the list
    
          End Select
        Next
      End Sub
    End Class
    What the form looks like after running the above code is attached.
    Attached Images Attached Images  
    Last edited by passel; Jul 22nd, 2014 at 06:53 PM.

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Thanks Jay and Passel. I'm doing some daft 12 hour days at work at the minute, however I'll have a look this weekend and let you know how I get on.

    Thanks once again for your help it as starting to click I think and make sense. If I have understood it you are saying why rebuild when I can just cross reference the original and output the difference?

  24. #24
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    I'm not sure anyone is saying that ("cross reference the original and output the difference").
    The code I've posted is essentially based on the same concept as jayinthe813's first post, and that is to select the stuff you want to keep from a line, rather than remove the stuff you don't want from the line.
    The main difference is his was just a quick example that assumed fixed field sizes for simplicity, and mine searches for the "," characters in the string to delineate the "fields".
    Selecting the stuff you want to keep in your output is much less work, in this case, than removing all the stuff you don't want.

    But, your output requirements do seem to keep evolving over time.
    For the "P" line, in post #1, you said the output should be:
    -- from --
    P,19180,||,01,17,P202503/008 1 Extende Top ,0900,0900,000000000001
    -- to --
    P,19180,P202503/008 1 Extende Top ,

    but in Post #15, you say the output should be: (note the begining "P," is missing and the trailing ",")
    19180, P202503/008 1 Extende Top
    (also note the space character between the "," and "P" in the second field. That space was not present in the original line.

    The code I posted in post #22, had to do "+ 1" in a few cases to ensure we picked up that trailing "," but now it seems you don't want it after all, which should make the code a little cleaner since you don't have to add the "+ 1" to include the "," in the output.

    Assuming that what you posted at the bottom of post #15 is what you want, without the "P," at the front of the "P" lines, then the below code produces that, and also doesn't include the trailing ",".
    It also does not insert the space between the "," and "P" of the second file, as that was not in the original line.
    If you want that space, then you can modify the code to add it.

    Since I had already added the "O" case to the code on my machine, I've left it in. I also changed to using a List of String to hold the output, which can easily be added to the Listbox in one AddRange method call (for echoing purposes, Listbox not needed for program to convert file).
    And, having it in a list makes writing the lines out to a file a one liner, as well.
    Code:
    Public Class Form1
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim lines() As String
        Dim sIdx, eIdx As Integer
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
        Dim sl As New List(Of String)
    
        lines = System.IO.File.ReadAllLines("c:\c\parse_in.txt")
    
        For i As Integer = 0 To lines.Count - 1
          With lines(i)
            Select Case .Substring(0, 2)
              Case "B,"
                sIdx = .IndexOf(",") + 1                 'Find first character after the first ","
                eIdx = .IndexOf(",", sIdx)               'Find next "," after that
                sl.Add(.Substring(sIdx, eIdx - sIdx))    'Add that substring (without trailing ",") to the list
    
              Case "P,"
                sb.Clear()
                eIdx = .IndexOf(",", 2)                  'find the next "," starting from the third character (2)
                sb.Append(.Substring(2, eIdx - 2))       'Append that to the stringbuilder obj, without the trailing ","
                sIdx = .IndexOf(",P")                    'Find where the ",P" starts (pick up the leading ",")
                eIdx = .IndexOf(",", sIdx + 1)           'Find the next "," after that 
                sb.Append(.Substring(sIdx, eIdx - sIdx)) 'Append it to the stringbuilder obj (without the trailing ",")
                sl.Add(sb.ToString)                      'Convert the stringbuilder obj to a string and add it to the list
    
              Case "O,"
                sIdx = 2                                 'Start from the 3rd character
                eIdx = .IndexOf(",", sIdx) + 1           'Find the next ","
                eIdx = .IndexOf(",", eIdx) + 1           'Find the next "," after that, include the ","
                sl.Add(.Substring(sIdx, eIdx - sIdx))    'Add that substring to the list
    
            End Select
          End With
        Next
        ListBox1.Items.Clear()
        ListBox1.Items.AddRange(sl.ToArray)
    
        System.IO.File.WriteAllLines("c:\c\parse_out.txt", sl)
    
      End Sub
    End Class
    Attached Images Attached Images  

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Wow that is absolutely perfect, thank you, really thank you a lot.

    Sorry about the confusion of the P. I am still unsure if it will be easier to understand on the shop floor with it there or not, it stands for piece and after the comma is the size. Although your post above does look great so it should be removed.

    This is probably pushing the limits now so I will apologise in advance. Is it at all possible using visual basic alter the code before NONOPT. The only reason being is that is the size for the opt in mm, however for some reason it is .1 of a mm. Is it possible or not logical to do the following

    3260,NONOPT
    17500,NONOPT

    into;

    326mm,NONOPT
    1750mm,NONOPT

  26. #26
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Well if you look at the "O," case, you'll see it finds the end of the first field, and then looks for the end of the second field
    Code:
                eIdx = .IndexOf(",", sIdx) + 1           'Find the next ","
                eIdx = .IndexOf(",", eIdx) + 1           'Find the next "," after that, include the ","
    (And, I didn't take off the + 1 there, so it includes the trailing "," which should have been removed, but I focused on the other lines before and didn't look at that one for some reason).

    In any case, since the end of the first field is found as an intermediate step to finding the end of the second, you can use that eIdx position, to select a shorter portion of that first field, and modify it.

    Since you will be modifying the string, and concatenating, I assume it will be better to reuse the string builder rather than doing multiple string operations.
    A modified version of the "O," case:
    Code:
              Case "O,"
                sIdx = 2                                     'Start from the 3rd character
                eIdx = .IndexOf(",", sIdx)                   'Find the next ","
                sb.Clear()                                   'Clear our stringBuilder obj
                sb.Append(.Substring(sIdx, eIdx - sIdx - 1)) 'don't include trailing "," and the digit before it
                sb.Append("mm")                              'add "mm"
                sIdx = eIdx                                  'set sIdx to start of next field, with leading ","
                eIdx = .IndexOf(",", sIdx + 1)               'Find the next "," after that, don't include the ","
                sb.Append(.Substring(sIdx, eIdx - sIdx))     'Add the second field, without trailing ","
                sl.Add(sb.ToString)                          'Add the resulting string to the list

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Thumbs up Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by passel View Post
    Well if you look at the "O," case, you'll see it finds the end of the first field, and then looks for the end of the second field
    Code:
                eIdx = .IndexOf(",", sIdx) + 1           'Find the next ","
                eIdx = .IndexOf(",", eIdx) + 1           'Find the next "," after that, include the ","
    (And, I didn't take off the + 1 there, so it includes the trailing "," which should have been removed, but I focused on the other lines before and didn't look at that one for some reason).

    In any case, since the end of the first field is found as an intermediate step to finding the end of the second, you can use that eIdx position, to select a shorter portion of that first field, and modify it.

    Since you will be modifying the string, and concatenating, I assume it will be better to reuse the string builder rather than doing multiple string operations.
    A modified version of the "O," case:
    Code:
              Case "O,"
                sIdx = 2                                     'Start from the 3rd character
                eIdx = .IndexOf(",", sIdx)                   'Find the next ","
                sb.Clear()                                   'Clear our stringBuilder obj
                sb.Append(.Substring(sIdx, eIdx - sIdx - 1)) 'don't include trailing "," and the digit before it
                sb.Append("mm")                              'add "mm"
                sIdx = eIdx                                  'set sIdx to start of next field, with leading ","
                eIdx = .IndexOf(",", sIdx + 1)               'Find the next "," after that, don't include the ","
                sb.Append(.Substring(sIdx, eIdx - sIdx))     'Add the second field, without trailing ","
                sl.Add(sb.ToString)                          'Add the resulting string to the list
    Thanks for your help once again, I will give it all try over the weekend and research how to then automatically send it to the default printer using Jay's link.

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Thumbs up Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Many thanks to everyone who has helped

    Code:
    Option Strict On
    Imports System.IO
    Imports System.Environment
    Public Class HVRoutForm
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSelectBatch.Click
            OpenF.InitialDirectory = "X:\jobs\Headvents"
            OpenF.FileName = "Select A Batch.."
            OpenF.Filter = "Only .449 Files (*.449) |*.449"
            OpenF.ShowDialog()
            Dim lines() As String
            Dim sIdx, eIdx As Integer
            Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
            Dim sl As New List(Of String)
            lines = System.IO.File.ReadAllLines(OpenF.FileName)
            For i As Integer = 0 To lines.Count - 1
                With lines(i)
                    Select Case .Substring(0, 2)
                        Case "B,"
                            sl.Add("Headvent Routing Sub batch")
                            sIdx = 5
                            eIdx = .IndexOf(",", sIdx)
                            sl.Add(.Substring(sIdx, eIdx - sIdx))
                        Case "P,"
                            sl.Add(" ")
                            sIdx = 2
                            eIdx = .IndexOf(",", sIdx)
                            sb.Clear()
                            sb.Append(.Substring(sIdx, eIdx - sIdx - 1))
                            sb.Append("mm")
                            sIdx = .IndexOf(",P")
                            eIdx = .IndexOf(",", sIdx + 1)
                            sb.Append(.Substring(sIdx, eIdx - sIdx - 28))
                            sl.Add(sb.ToString)
                        Case "O,"
                            sIdx = 2
                            eIdx = .IndexOf(",", sIdx)
                            sb.Clear()
                            sb.Append(.Substring(sIdx, eIdx - sIdx - 1))
                            sb.Append("mm")
                            sIdx = eIdx
                            eIdx = .IndexOf(",", sIdx + 1)
                            sb.Append(.Substring(sIdx, eIdx - sIdx))
                            sl.Add(sb.ToString)
                    End Select
                End With
            Next
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(sl.ToArray)
            System.IO.File.WriteAllLines(OpenF.FileName, sl)
        End Sub
    End Class
    Name:  Untitled.jpg
Views: 151
Size:  52.7 KB

    Think I'm finally starting to understand it, really can't thank you enough.

    Just starting to research printing to the default printer now.

  29. #29
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    The thing that "jumped out" at me looking at your implementation was this line:
    sb.Append(.Substring(sIdx, eIdx - sIdx - 28))

    Subtracting that large (28) literal constant seems like it might fail if the line wasn't formatted in a very particular way.
    And indeed, when run against the example input you provided in the first post, it did fail because the resulting value (eIdx - sIdx - 28) was less than zero.
    If you don't want the data after the space in that line, then I would change those two lines in the "P," case, the first to look for the next " " space character instead of the "," comma, then you don't have to subtract anything off of the eIdx.
    Code:
              Case "P,"
                sl.Add(" ")
                sIdx = 2
                eIdx = .IndexOf(",", sIdx)
                sb.Clear()
                sb.Append(.Substring(sIdx, eIdx - sIdx - 1))
                sb.Append("mm")
                sIdx = .IndexOf(",P")
                ' eIdx = .IndexOf(",", sIdx + 1)
                ' sb.Append(.Substring(sIdx, eIdx - sIdx - 28))
                eIdx = .IndexOf(" ", sIdx)                   'look for the next space
                sb.Append(.Substring(sIdx, eIdx - sIdx))     'Append substring upto, but not including that space
                sl.Add(sb.ToString)
    p.s. the other "scary" part to me, is the fact that you overwrite your input file with the output.
    Of course you probably want it that way, but I don't usually do that in case something went wrong I would still have the original file to try again, after I fixed whatever issue there was.

    p.p.s. I guess if you're doing research on Printing then you will probably have already come across a page like this one. I haven't tried it myself, but it does seem to hint that some of the examples from Microsoft might not actually work correctly. In any case, there seems to be plenty of information out there, and as I haven't printed from VB.Net myself yet, I'll leave it at that.
    Last edited by passel; Jul 25th, 2014 at 02:47 PM.

  30. #30

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by passel View Post
    The thing that "jumped out" at me looking at your implementation was this line:
    sb.Append(.Substring(sIdx, eIdx - sIdx - 28))

    Subtracting that large (28) literal constant seems like it might fail if the line wasn't formatted in a very particular way.
    And indeed, when run against the example input you provided in the first post, it did fail because the resulting value (eIdx - sIdx - 28) was less than zero.
    If you don't want the data after the space in that line, then I would change those two lines in the "P," case, the first to look for the next " " space character instead of the "," comma, then you don't have to subtract anything off of the eIdx.
    Code:
              Case "P,"
                sl.Add(" ")
                sIdx = 2
                eIdx = .IndexOf(",", sIdx)
                sb.Clear()
                sb.Append(.Substring(sIdx, eIdx - sIdx - 1))
                sb.Append("mm")
                sIdx = .IndexOf(",P")
                ' eIdx = .IndexOf(",", sIdx + 1)
                ' sb.Append(.Substring(sIdx, eIdx - sIdx - 28))
                eIdx = .IndexOf(" ", sIdx)                   'look for the next space
                sb.Append(.Substring(sIdx, eIdx - sIdx))     'Append substring upto, but not including that space
                sl.Add(sb.ToString)
    p.s. the other "scary" part to me, is the fact that you overwrite your input file with the output.
    Of course you probably want it that way, but I don't usually do that in case something went wrong I would still have the original file to try again, after I fixed whatever issue there was.

    p.p.s. I guess if you're doing research on Printing then you will probably have already come across a page like this one. I haven't tried it myself, but it does seem to hint that some of the examples from Microsoft might not actually work correctly. In any case, there seems to be plenty of information out there, and as I haven't printed from VB.Net myself yet, I'll leave it at that.
    Yeah sorry about my dodging coding, I was trying to play with your code to understand it better.

    I don't really want to overwrite the original just print however it doesn't matter too much if I do overwrite as it's a simply process to recreate the file.

    Thanks for the link it looks promising.

    One final thing

    Where a size starts with a "0" is there a way to remove it from the P case?

    1450etc I want to stay

    0450 I want to be 450

  31. #31
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    I don't know about the "P" case, but I see in the "R" case sometimes you have 1 or 2 leading zeros.
    The simplest approach it would seem to me is to convert the numeric part of the string to a number, then convert the number back to a string.
    .Net has a parse and TryParse methods to convert a string to a number, but they are sort of bulky compared to the old Val function, and I don't mind that Val will return 0 if the string is not a number in most of my uses.
    So, instead of appending the substring of that portion to the string builder object, use Val() on the substring to make it a number, then append the number converted back to a string to the string builder
    Code:
    'At the top of the code method where the rest of the declarations are add a variable to hold the converted value
      Dim tVal as Integer
    
    'Then down where Case "P," is processed, replace the one sb.append line with the tVal conversion and the append(tVal) lines
              Case "P,"
                sl.Add(" ")
                sIdx = 2
                eIdx = .IndexOf(",", sIdx)
                sb.Clear()
                ' sb.Append(.Substring(sIdx, eIdx - sIdx - 1))       'Commented out original line
                tVal = CInt(Val(.Substring(sIdx, eIdx - sIdx - 1)))  'Convert numeric string to a number
                sb.Append(tVal.ToString)                             'Convert number back to string (removes leading 0's)
                sb.Append("mm")
                sIdx = .IndexOf(",P")
                eIdx = .IndexOf(" ", sIdx)                   'look for the next space
                sb.Append(.Substring(sIdx, eIdx - sIdx))     'Append substring upto, but not including that space
                sl.Add(sb.ToString)

  32. #32

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Quote Originally Posted by passel View Post
    I don't know about the "P" case, but I see in the "R" case sometimes you have 1 or 2 leading zeros.
    The simplest approach it would seem to me is to convert the numeric part of the string to a number, then convert the number back to a string.
    .Net has a parse and TryParse methods to convert a string to a number, but they are sort of bulky compared to the old Val function, and I don't mind that Val will return 0 if the string is not a number in most of my uses.
    So, instead of appending the substring of that portion to the string builder object, use Val() on the substring to make it a number, then append the number converted back to a string to the string builder
    Code:
    'At the top of the code method where the rest of the declarations are add a variable to hold the converted value
      Dim tVal as Integer
    
    'Then down where Case "P," is processed, replace the one sb.append line with the tVal conversion and the append(tVal) lines
              Case "P,"
                sl.Add(" ")
                sIdx = 2
                eIdx = .IndexOf(",", sIdx)
                sb.Clear()
                ' sb.Append(.Substring(sIdx, eIdx - sIdx - 1))       'Commented out original line
                tVal = CInt(Val(.Substring(sIdx, eIdx - sIdx - 1)))  'Convert numeric string to a number
                sb.Append(tVal.ToString)                             'Convert number back to string (removes leading 0's)
                sb.Append("mm")
                sIdx = .IndexOf(",P")
                eIdx = .IndexOf(" ", sIdx)                   'look for the next space
                sb.Append(.Substring(sIdx, eIdx - sIdx))     'Append substring upto, but not including that space
                sl.Add(sb.ToString)
    You sir are a genius!

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Jul 2014
    Posts
    17

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    I've got it to print, however it only prints a maximum of one page. I'll keep trying at it.

    One problem I do have is that if someone selects a file that has already been done the program crashes, I want a waning to come up to tell the operator the batch has already been done?

    my code
    Code:
    Option Strict On
    Imports System.IO
    Imports System.Environment
    Imports System.Drawing
    Imports System.Drawing.Printing
    Public Class HVRoutForm
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSelectBatch.Click
            OpenF.InitialDirectory = "X:\jobs\Headvents"
            OpenF.FileName = "Select A Batch.."
            OpenF.Filter = "Only .449 Files (*.449) |*.449"
            OpenF.ShowDialog()
            Dim lines() As String
            Dim sIdx, eIdx As Integer 'Start index and end index
            Dim tVal As Integer 'tVal to change text to number
            Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
            Dim sl As New List(Of String) 'sl - string list
            lines = System.IO.File.ReadAllLines(OpenF.FileName)
            For i As Integer = 0 To lines.Count - 1
                With lines(i)
                    Select Case .Substring(0, 2)
                        Case "B," 'line starting with B
                            sl.Add("Headvent Routing Sub batch") 'Add text before
                            sIdx = 5 'Start at 6th character
                            eIdx = .IndexOf(",", sIdx) 'add the information between the 6th character ad the next comma
                            sl.Add(.Substring(sIdx, eIdx - sIdx)) 'add it to string with out comma after
                        Case "P," 'line starting with P
                            sl.Add(" ") 'Add text before each piece
                            sIdx = 2 'Start at 3rd
                            eIdx = .IndexOf(",", sIdx) 'to next comma
                            sb.Clear() 'clear string builder?
                            tVal = CInt(Val(.Substring(sIdx, eIdx - sIdx - 1))) 'convert piece size to a number
                            sb.Append(tVal.ToString) 'convert it back to a string to get rid of any 0 at start
                            sb.Append("mm") 'add mm to size
                            sIdx = .IndexOf(",P") 'start at P
                            eIdx = .IndexOf(" ", sIdx) 'to next empty space after to get rid of extender info
                            sb.Append(.Substring(sIdx, eIdx - sIdx)) 'add to string wthout comma after
                            sl.Add(sb.ToString) 'convert stringbuilder to string to add to list?
                        Case "O," 'Line starting with o
                            sIdx = 2 'start at 3rd
                            eIdx = .IndexOf(",", sIdx) 'to next comma
                            sb.Clear() 'empty string builder?
                            sb.Append(.Substring(sIdx, eIdx - sIdx - 1)) 'add
                            sb.Append("mm") 'add mm to size
                            sIdx = eIdx 'set start index to next comma
                            eIdx = .IndexOf(",", sIdx + 1) 'find the next comma after that but don't include end comma
                            sb.Append(.Substring(sIdx, eIdx - sIdx)) 'add without comma after
                            sb.Append(" t/vent") 'add text after 2000/4000
                            sl.Add(sb.ToString) 'add to string list
                    End Select
                End With
            Next
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(sl.ToArray)
            System.IO.File.WriteAllLines(OpenF.FileName, sl)
        End Sub
    
        Private Sub PrintBtn_Click(sender As Object, e As EventArgs) Handles PrintBtn.Click
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim f As New Font("Calibri", 12, FontStyle.Regular)
            Dim x As Single, y As Single
            Dim h As Single = 30
            x = e.MarginBounds.Left
            y = e.MarginBounds.Top
            Dim item As String
            For Each item In ListBox1.Items
                e.Graphics.DrawString(item, f, Brushes.Black, x, y)
                y += h
            Next
        End Sub
    End Class

  34. #34
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    Is your space bar broken? Please think about readable code. I have not read your code but think about this. If you do an action and want to check that the action has not been done you use a collection.

  35. #35
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Using visual basic + windows form to remove unnecassary lines in a text file

    It seems like you could just write something on the first line when you create the output file that you could check for, and not process the file if it is already there.
    For instance, it looks like you print "Headvent Routing Sub batch" on the first line, so you could check for that, or even just the first word "Headvent", and if that is on the first line of the file, it has been processed already.

    But, it also seems you could change the output file name extension so that it would be selectable again as an input.

Posting Permissions

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



Click Here to Expand Forum to Full Width