vb.net Split Text file into different files by section












-1















I have a lot of text files that have the same structure



They each have an unknown number of sections



All sections names are known. They are stored as an Array.



Dim alpha() As String = {"Section 1", "Section 2", "Section 3"}


The sections are just plain text without any symbol to help identify. But the text below them has all kinds of symbols and text.



:
ex.



Section 1



There is a lot of text with symbols and numbers and newLines.



Section 2



There is a lot of text with symbols and numbers and newLines.



Section 3



There is a lot of text with symbols and numbers and newLines.



Now. There is no way to get this with regex cause each section is different and can't match with anything.
I want to save each section into a different file. If you can help me to figure out how to get, maybe, an array for each section for each different file i can do the saving part.
Any ideas?










share|improve this question




















  • 2





    Please add an example of the text file and how do you want it to be splitted - into lines, words ,etc.

    – preciousbetine
    Nov 25 '18 at 4:12






  • 1





    Thats any example abode. An know title and then an unknow number of lines that have unknow text.

    – Paul Mantziaris
    Nov 25 '18 at 5:13













  • Post the text with the symbols so we can know what delimiters to use

    – preciousbetine
    Nov 25 '18 at 5:26
















-1















I have a lot of text files that have the same structure



They each have an unknown number of sections



All sections names are known. They are stored as an Array.



Dim alpha() As String = {"Section 1", "Section 2", "Section 3"}


The sections are just plain text without any symbol to help identify. But the text below them has all kinds of symbols and text.



:
ex.



Section 1



There is a lot of text with symbols and numbers and newLines.



Section 2



There is a lot of text with symbols and numbers and newLines.



Section 3



There is a lot of text with symbols and numbers and newLines.



Now. There is no way to get this with regex cause each section is different and can't match with anything.
I want to save each section into a different file. If you can help me to figure out how to get, maybe, an array for each section for each different file i can do the saving part.
Any ideas?










share|improve this question




















  • 2





    Please add an example of the text file and how do you want it to be splitted - into lines, words ,etc.

    – preciousbetine
    Nov 25 '18 at 4:12






  • 1





    Thats any example abode. An know title and then an unknow number of lines that have unknow text.

    – Paul Mantziaris
    Nov 25 '18 at 5:13













  • Post the text with the symbols so we can know what delimiters to use

    – preciousbetine
    Nov 25 '18 at 5:26














-1












-1








-1








I have a lot of text files that have the same structure



They each have an unknown number of sections



All sections names are known. They are stored as an Array.



Dim alpha() As String = {"Section 1", "Section 2", "Section 3"}


The sections are just plain text without any symbol to help identify. But the text below them has all kinds of symbols and text.



:
ex.



Section 1



There is a lot of text with symbols and numbers and newLines.



Section 2



There is a lot of text with symbols and numbers and newLines.



Section 3



There is a lot of text with symbols and numbers and newLines.



Now. There is no way to get this with regex cause each section is different and can't match with anything.
I want to save each section into a different file. If you can help me to figure out how to get, maybe, an array for each section for each different file i can do the saving part.
Any ideas?










share|improve this question
















I have a lot of text files that have the same structure



They each have an unknown number of sections



All sections names are known. They are stored as an Array.



Dim alpha() As String = {"Section 1", "Section 2", "Section 3"}


The sections are just plain text without any symbol to help identify. But the text below them has all kinds of symbols and text.



:
ex.



Section 1



There is a lot of text with symbols and numbers and newLines.



Section 2



There is a lot of text with symbols and numbers and newLines.



Section 3



There is a lot of text with symbols and numbers and newLines.



Now. There is no way to get this with regex cause each section is different and can't match with anything.
I want to save each section into a different file. If you can help me to figure out how to get, maybe, an array for each section for each different file i can do the saving part.
Any ideas?







vb.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 15:05









David Wilson

3,87321326




3,87321326










asked Nov 25 '18 at 3:27









Paul MantziarisPaul Mantziaris

204




204








  • 2





    Please add an example of the text file and how do you want it to be splitted - into lines, words ,etc.

    – preciousbetine
    Nov 25 '18 at 4:12






  • 1





    Thats any example abode. An know title and then an unknow number of lines that have unknow text.

    – Paul Mantziaris
    Nov 25 '18 at 5:13













  • Post the text with the symbols so we can know what delimiters to use

    – preciousbetine
    Nov 25 '18 at 5:26














  • 2





    Please add an example of the text file and how do you want it to be splitted - into lines, words ,etc.

    – preciousbetine
    Nov 25 '18 at 4:12






  • 1





    Thats any example abode. An know title and then an unknow number of lines that have unknow text.

    – Paul Mantziaris
    Nov 25 '18 at 5:13













  • Post the text with the symbols so we can know what delimiters to use

    – preciousbetine
    Nov 25 '18 at 5:26








2




2





Please add an example of the text file and how do you want it to be splitted - into lines, words ,etc.

– preciousbetine
Nov 25 '18 at 4:12





Please add an example of the text file and how do you want it to be splitted - into lines, words ,etc.

– preciousbetine
Nov 25 '18 at 4:12




1




1





Thats any example abode. An know title and then an unknow number of lines that have unknow text.

– Paul Mantziaris
Nov 25 '18 at 5:13







Thats any example abode. An know title and then an unknow number of lines that have unknow text.

– Paul Mantziaris
Nov 25 '18 at 5:13















Post the text with the symbols so we can know what delimiters to use

– preciousbetine
Nov 25 '18 at 5:26





Post the text with the symbols so we can know what delimiters to use

– preciousbetine
Nov 25 '18 at 5:26












1 Answer
1






active

oldest

votes


















0














Strictly speaking, Stackoverflow isnt a code writing service, but I couldn't resist



The code below will do what you ask.



Dim fn As String = "K:sectiontest.txt"
Dim completeFile As String()
Dim sectionsPostSplitting As String()()

Private Function GetSectionIndexes() As List(Of Integer)
Dim SectionIndexes As New List(Of Integer)
For i As Integer = 0 To completeFile.Count - 1
If completeFile(i).StartsWith("Section") Then
SectionIndexes.Add(i)
End If
Next
Return SectionIndexes
End Function

Private Function GetSectionText(i As Integer, temptxtarray As String()) As String()
Dim sectionText As New List(Of String)
Dim j As Integer = i
j += 1
While (j < temptxtarray.Length - 1) And (Not temptxtarray(j).StartsWith("Section"))
sectionText.Add(temptxtarray(j))
j += 1
End While
Return sectionText.ToArray
End Function

Private Function SplitCompleteText(txt As String()) As String()()
Dim sectionsText As New List(Of String())
Dim sectionIndexes As List(Of Integer) = GetSectionIndexes()
For i As Integer = 0 To sectionIndexes.Count - 1
sectionsText.Add(GetSectionText(sectionIndexes(i), completeFile))
Next
Return sectionsText.ToArray
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
completeFile = File.ReadAllLines(fn)
sectionsPostSplitting = SplitCompleteText(completeFile)
End Sub


The line..



sectionsPostSplitting = SplitCompleteText(completeFile)


in the button.click event handler will leave you with an array with each item in the array being an array of strings equalling the text from each section without the section header. So sectionsPostSplitting(0) will be an array of one line strings for section 1 and so on.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464396%2fvb-net-split-text-file-into-different-files-by-section%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Strictly speaking, Stackoverflow isnt a code writing service, but I couldn't resist



    The code below will do what you ask.



    Dim fn As String = "K:sectiontest.txt"
    Dim completeFile As String()
    Dim sectionsPostSplitting As String()()

    Private Function GetSectionIndexes() As List(Of Integer)
    Dim SectionIndexes As New List(Of Integer)
    For i As Integer = 0 To completeFile.Count - 1
    If completeFile(i).StartsWith("Section") Then
    SectionIndexes.Add(i)
    End If
    Next
    Return SectionIndexes
    End Function

    Private Function GetSectionText(i As Integer, temptxtarray As String()) As String()
    Dim sectionText As New List(Of String)
    Dim j As Integer = i
    j += 1
    While (j < temptxtarray.Length - 1) And (Not temptxtarray(j).StartsWith("Section"))
    sectionText.Add(temptxtarray(j))
    j += 1
    End While
    Return sectionText.ToArray
    End Function

    Private Function SplitCompleteText(txt As String()) As String()()
    Dim sectionsText As New List(Of String())
    Dim sectionIndexes As List(Of Integer) = GetSectionIndexes()
    For i As Integer = 0 To sectionIndexes.Count - 1
    sectionsText.Add(GetSectionText(sectionIndexes(i), completeFile))
    Next
    Return sectionsText.ToArray
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    completeFile = File.ReadAllLines(fn)
    sectionsPostSplitting = SplitCompleteText(completeFile)
    End Sub


    The line..



    sectionsPostSplitting = SplitCompleteText(completeFile)


    in the button.click event handler will leave you with an array with each item in the array being an array of strings equalling the text from each section without the section header. So sectionsPostSplitting(0) will be an array of one line strings for section 1 and so on.






    share|improve this answer




























      0














      Strictly speaking, Stackoverflow isnt a code writing service, but I couldn't resist



      The code below will do what you ask.



      Dim fn As String = "K:sectiontest.txt"
      Dim completeFile As String()
      Dim sectionsPostSplitting As String()()

      Private Function GetSectionIndexes() As List(Of Integer)
      Dim SectionIndexes As New List(Of Integer)
      For i As Integer = 0 To completeFile.Count - 1
      If completeFile(i).StartsWith("Section") Then
      SectionIndexes.Add(i)
      End If
      Next
      Return SectionIndexes
      End Function

      Private Function GetSectionText(i As Integer, temptxtarray As String()) As String()
      Dim sectionText As New List(Of String)
      Dim j As Integer = i
      j += 1
      While (j < temptxtarray.Length - 1) And (Not temptxtarray(j).StartsWith("Section"))
      sectionText.Add(temptxtarray(j))
      j += 1
      End While
      Return sectionText.ToArray
      End Function

      Private Function SplitCompleteText(txt As String()) As String()()
      Dim sectionsText As New List(Of String())
      Dim sectionIndexes As List(Of Integer) = GetSectionIndexes()
      For i As Integer = 0 To sectionIndexes.Count - 1
      sectionsText.Add(GetSectionText(sectionIndexes(i), completeFile))
      Next
      Return sectionsText.ToArray
      End Function

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      completeFile = File.ReadAllLines(fn)
      sectionsPostSplitting = SplitCompleteText(completeFile)
      End Sub


      The line..



      sectionsPostSplitting = SplitCompleteText(completeFile)


      in the button.click event handler will leave you with an array with each item in the array being an array of strings equalling the text from each section without the section header. So sectionsPostSplitting(0) will be an array of one line strings for section 1 and so on.






      share|improve this answer


























        0












        0








        0







        Strictly speaking, Stackoverflow isnt a code writing service, but I couldn't resist



        The code below will do what you ask.



        Dim fn As String = "K:sectiontest.txt"
        Dim completeFile As String()
        Dim sectionsPostSplitting As String()()

        Private Function GetSectionIndexes() As List(Of Integer)
        Dim SectionIndexes As New List(Of Integer)
        For i As Integer = 0 To completeFile.Count - 1
        If completeFile(i).StartsWith("Section") Then
        SectionIndexes.Add(i)
        End If
        Next
        Return SectionIndexes
        End Function

        Private Function GetSectionText(i As Integer, temptxtarray As String()) As String()
        Dim sectionText As New List(Of String)
        Dim j As Integer = i
        j += 1
        While (j < temptxtarray.Length - 1) And (Not temptxtarray(j).StartsWith("Section"))
        sectionText.Add(temptxtarray(j))
        j += 1
        End While
        Return sectionText.ToArray
        End Function

        Private Function SplitCompleteText(txt As String()) As String()()
        Dim sectionsText As New List(Of String())
        Dim sectionIndexes As List(Of Integer) = GetSectionIndexes()
        For i As Integer = 0 To sectionIndexes.Count - 1
        sectionsText.Add(GetSectionText(sectionIndexes(i), completeFile))
        Next
        Return sectionsText.ToArray
        End Function

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        completeFile = File.ReadAllLines(fn)
        sectionsPostSplitting = SplitCompleteText(completeFile)
        End Sub


        The line..



        sectionsPostSplitting = SplitCompleteText(completeFile)


        in the button.click event handler will leave you with an array with each item in the array being an array of strings equalling the text from each section without the section header. So sectionsPostSplitting(0) will be an array of one line strings for section 1 and so on.






        share|improve this answer













        Strictly speaking, Stackoverflow isnt a code writing service, but I couldn't resist



        The code below will do what you ask.



        Dim fn As String = "K:sectiontest.txt"
        Dim completeFile As String()
        Dim sectionsPostSplitting As String()()

        Private Function GetSectionIndexes() As List(Of Integer)
        Dim SectionIndexes As New List(Of Integer)
        For i As Integer = 0 To completeFile.Count - 1
        If completeFile(i).StartsWith("Section") Then
        SectionIndexes.Add(i)
        End If
        Next
        Return SectionIndexes
        End Function

        Private Function GetSectionText(i As Integer, temptxtarray As String()) As String()
        Dim sectionText As New List(Of String)
        Dim j As Integer = i
        j += 1
        While (j < temptxtarray.Length - 1) And (Not temptxtarray(j).StartsWith("Section"))
        sectionText.Add(temptxtarray(j))
        j += 1
        End While
        Return sectionText.ToArray
        End Function

        Private Function SplitCompleteText(txt As String()) As String()()
        Dim sectionsText As New List(Of String())
        Dim sectionIndexes As List(Of Integer) = GetSectionIndexes()
        For i As Integer = 0 To sectionIndexes.Count - 1
        sectionsText.Add(GetSectionText(sectionIndexes(i), completeFile))
        Next
        Return sectionsText.ToArray
        End Function

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        completeFile = File.ReadAllLines(fn)
        sectionsPostSplitting = SplitCompleteText(completeFile)
        End Sub


        The line..



        sectionsPostSplitting = SplitCompleteText(completeFile)


        in the button.click event handler will leave you with an array with each item in the array being an array of strings equalling the text from each section without the section header. So sectionsPostSplitting(0) will be an array of one line strings for section 1 and so on.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 18:22









        David WilsonDavid Wilson

        3,87321326




        3,87321326
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464396%2fvb-net-split-text-file-into-different-files-by-section%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen