PDA

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



ucan
09-10-2022, 04:40 AM
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.

Logit
09-10-2022, 08:26 AM
Your description is kinda confusing. Are you saying you want to copy the row that contains certain text ? Not copy any other rows ?

Aussiebear
09-10-2022, 12:40 PM
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.

ucan
09-10-2022, 02:33 PM
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.




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.

Logit
09-10-2022, 02:48 PM
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.

ucan
09-10-2022, 04:49 PM
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.

Aussiebear
09-10-2022, 06:10 PM
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

snb
09-11-2022, 03:36 AM
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

ucan
09-11-2022, 04:56 AM
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.



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