Consulting

Results 1 to 9 of 9

Thread: How to split a big text file (with more than 500000 lines) in to smaller files

  1. #1
    VBAX Regular
    Joined
    Feb 2022
    Posts
    24
    Location

    How to split a big text file (with more than 500000 lines) in to smaller files

    Hi,
    I'm looking for some vba that will do the following:
    1. create smaller text files from a larger one
    2. I simply require a specific section of the text file (its a log file).
    3. The plan is to look for a particular key word in a line, find the key word, and then begin the copy including that line of key word of text.
    4. If same key word is found, the copy should end before that line.
    5. After that, the copied text was transferred to a new text file.
    The original file will be located under C:\Temp.
    Last edited by ucan; 09-10-2022 at 09:38 AM. Reason: trying to remove confusion

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    Your description is kinda confusing. Are you saying you want to copy the row that contains certain text ? Not copy any other rows ?

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Quote Originally Posted by ucan View Post
    Hi,
    I'm looking for some vba that will do the following:
    1. create smaller text files from a larger one
    Did you supply an example for us to examine?

    2. I simply require a specific section of the text file (its a log file).
    3. The plan is to look for a particular key word in a line, find the key word, and then begin the copy including that line of key word of text.
    4. If same key word is found, the copy should end before that line.
    Have you provided an example of the "key" word?

    5. After that, the copied text was transferred to a new text file.
    The original file will be located under C:\Temp.
    Did you indicate where the new text file will be stored?

    ucan, please stop and have a good think about the post you presented. As Logit suggested your post is confusing, since at this stage its merely a concept that you seemingly understand, but we are just guessing at.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Regular
    Joined
    Feb 2022
    Posts
    24
    Location
    Attached is a sample file and ". following:" is the keyword in given lines.
    In the code I request for two keywords which may be same or different which I can change.
    Hope it is clear now. My apologies.



    Quote Originally Posted by Aussiebear View Post
    Did you supply an example for us to examine?



    Have you provided an example of the "key" word?



    Did you indicate where the new text file will be stored?

    ucan, please stop and have a good think about the post you presented. As Logit suggested your post is confusing, since at this stage its merely a concept that you seemingly understand, but we are just guessing at.
    Attached Files Attached Files

  5. #5
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    And what is it you want copied ? Just the line that has ". following:" .... or is there something else you want copied ?

    Also you listed one keyword and then you mention two key words. What are the keywords you are searching for .... or is there really only the one ?


    You continue to make things confusing.
    Last edited by Logit; 09-10-2022 at 03:02 PM.

  6. #6
    VBAX Regular
    Joined
    Feb 2022
    Posts
    24
    Location
    The keyword is only one i.e., ". following:".
    I want to copy all text/numbers/special characters whatever is there between the keyword (". following:" and ". following:") to create a new file.
    Each file must have the first line where the
    keyword ". following:" appears but exclude that (keyword: ". following:" ) in last line because next file data will start from there. I hope it is not confusing anymore.

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Here is the example as created by ucan.

    file1. I'm looking for some vba that will do the . following:*1. create smaller text files from a larger one
    2. I simply require a specific section of the text file (its a log file).
    3. The plan is to look for a particular key word in a line,
    find the key word, and then begin the copy including that line of key word of text.
    4. If same key word is found, the copy should end before
    that line.
    5. After that, the copied text was transferred to a new
    text file.
    The original file will be located under C:\Temp.
    1. create smaller text files from a larger one
    2. I simply require a specific section of the text file (its a log file).
    3. The plan is to look for a particular key word in a line,
    find the key word, and then begin the copy including that line of key word of text.
    4. If same key word is found, the copy should end before
    that line.
    5. After that, the copied text was transferred to a new
    text file.
    The original file will be located under C:\Temp.
    #####
    3. The plan is to look for a particular key word in a line,
    find the key word, and then begin the copy including that line of key word of text.
    4. If same key word is found, the copy should end before
    that line.
    5. After that, the copied text was transferred to a new
    text file.
    The original file will be located under C:\Temp.


    1. create smaller text files from a larger one 2. I simply require a specific section of the text file (its a log file).
    3. The plan is to look for a particular key word in a line,
    find the key word, and then begin the copy including that line of key word of text. 4. If same key word is found, the copy should end before
    that line.
    5. After that, the copied text was transferred to a new text file.
    The original file will be located under C:\Temp.
    #####
    ----------------------------------------------------------------------------------------above text will be file1 in C:\Temp

    2. do the . following: I'm looking for some vba that will
    1. create smaller text files from a larger one
    2. I simply require a specific section of the text file (its a log file).
    3. The plan is to look for a particular key word in a line, find the key
    word, and then begin the copy including that
    line of key word of text.
    4. If same key word is found, the copy should end before that line.
    5. After that, the copied text was transferred to a new text file.
    The original file will be located under C:\Temp.
    1. create smaller text files from a larger one
    2. I simply require a specific section of the text file (its a log file).
    3. The plan is to look for a particular key word in a line,
    find the key word, and then begin the copy including that line of key word of text.
    4. If same key word is found, the copy should end before
    that line.
    5. After that, the copied text was transferred to a new
    text file.
    The original file will be located under C:\Temp.
    1. create smaller text files from a larger one
    2. I simply require a specific section of the text file (its a log file).
    3. The plan is to look for a particular key word in a line,
    find the key word, and then begin the copy including that line of key word of text.
    4. If same key word is found, the copy should end before
    that line.
    5. After that, the copied text was transferred to a new
    text file.
    The original file will be located under C:\Temp.
    ----------------------------------------------------------------------------------------above text will be file2 in C:\Temp

    3.I'm looking for some vba that will do the . following:
    1. create smaller text files from a larger one
    2. I simply require a specific section of the text file (its a log file).
    3. The plan is to look for a particular key word in a line, find the key word,
    and then begin the copy including that line of key word of text.
    4. If same key word is found, the copy should end before that line.
    5. After that, the copied text was transferred to a new text file.
    The original file will be located under C:\Temp.
    ----------------------------------------------------------------------------------------above text will be file3 in C:\Temp
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
      With CreateObject("scripting.filesystemobject")
        sn = Split(.opentextfile("G:\OF\vba_text2file.log").readall, ". following")
      
        For j = 1 To UBound(sn)
          sp = Split(sn(j - 1), vbCrLf)
          sn(j - 1) = Left(sn(j - 1), Len(sn(j - 1)) - Len(sp(UBound(sp))))
          sn(j) = sp(UBound(sp)) & ". following" & sn(j)
          .createtextfile("G:\OF\file_" & j & ".txt").write sn(j - 1)
        Next
      End With
    End Sub

  9. #9
    VBAX Regular
    Joined
    Feb 2022
    Posts
    24
    Location
    Thank you. worked perfectly but
    1. file_1 is empty
    2. file_1 data came in file_2
    3. The last file do not have have the keyword in last line so to be last file is missing.

    Quote Originally Posted by snb View Post
    Sub M_snb()
      With CreateObject("scripting.filesystemobject")
        sn = Split(.opentextfile("G:\OF\vba_text2file.log").readall, ". following")
      
        For j = 1 To UBound(sn)
          sp = Split(sn(j - 1), vbCrLf)
          sn(j - 1) = Left(sn(j - 1), Len(sn(j - 1)) - Len(sp(UBound(sp))))
          sn(j) = sp(UBound(sp)) & ". following" & sn(j)
          .createtextfile("G:\OF\file_" & j & ".txt").write sn(j - 1)
        Next
      End With
    End Sub

Tags for this Thread

Posting Permissions

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