PDA

View Full Version : wrap broken lines in one line in a given text or log file



ucan
09-11-2022, 08:36 AM
How to wrap broken lines in one line in a given big text file ?

Example input text file:

1. 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

Example output text file:

1. 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-13-2022, 01:53 AM
How can we help if you have no notion of VBA at all ?

georgiboy
09-13-2022, 02:52 AM
There is quite a lot of detail missing to be able to put something together that will do the job so instead i have created my own text file and keyword.
The example below will pull everything out of the text file in between the words 'Apple' & 'Apple', hold it in an array and then dump it in a new text file saved to the same path.

The import file has been named 'Input"
The file it will create will be named 'Output'

You should be able to use the logic to do what you need.


Sub test()
Dim FileToRead As Scripting.TextStream
Dim FileToWrite As Scripting.TextStream
Dim TextString As String
Dim FSO As New FileSystemObject
Dim kWord As String, x As Long, y As Long
Dim var As Variant
Dim txtFilePath As String
Dim oVar() As Variant

kWord = "Apple"
txtFilePath = "C:\Users\jbloggs\Desktop\test\"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set FileToRead = FSO.OpenTextFile(txtFilePath & "Input.txt", ForReading)
TextString = FileToRead.ReadAll
FileToRead.Close
var = Split(TextString, kWord)
For x = 1 To UBound(var) Step 2
ReDim Preserve oVar(y)
oVar(y) = var(x)
y = y + 1
Next x
Set FileToWrite = FSO.CreateTextFile(txtFilePath & "Output.txt", True)
FileToWrite.WriteLine Join(oVar, vbNewLine)
FileToWrite.Close
End Sub

Output file has been uploaded for reference

ucan
09-13-2022, 08:52 AM
Hi,


Dim FileToRead As Scripting.TextStream
Showing Compile Error: User-Defined type not defined.


There is quite a lot of detail missing to be able to put something together that will do the job so instead i have created my own text file and keyword.
The example below will pull everything out of the text file in between the words 'Apple' & 'Apple', hold it in an array and then dump it in a new text file saved to the same path.

The import file has been named 'Input"
The file it will create will be named 'Output'

You should be able to use the logic to do what you need.


Sub test()
Dim FileToRead As Scripting.TextStream
Dim FileToWrite As Scripting.TextStream
Dim TextString As String
Dim FSO As New FileSystemObject
Dim kWord As String, x As Long, y As Long
Dim var As Variant
Dim txtFilePath As String
Dim oVar() As Variant

kWord = "Apple"
txtFilePath = "C:\Users\jbloggs\Desktop\test\"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set FileToRead = FSO.OpenTextFile(txtFilePath & "Input.txt", ForReading)
TextString = FileToRead.ReadAll
FileToRead.Close
var = Split(TextString, kWord)
For x = 1 To UBound(var) Step 2
ReDim Preserve oVar(y)
oVar(y) = var(x)
y = y + 1
Next x
Set FileToWrite = FSO.CreateTextFile(txtFilePath & "Output.txt", True)
FileToWrite.WriteLine Join(oVar, vbNewLine)
FileToWrite.Close
End Sub

Output file has been uploaded for reference

ucan
09-13-2022, 08:56 AM
Actually I want to "remove-line-breaks". But I need the out as shown.
Here is an online tool with default settings that does the same: https://www.gillmeister-software.com/online-tools/text/remove-line-breaks.aspx
But my text file is big so I need help to do it with VBA.


How can we help if you have no notion of VBA at all ?

georgiboy
09-13-2022, 09:13 AM
You will need to add a reference to the Scripting Runtime in the VBE for it to work.

snb
09-13-2022, 11:27 AM
You'd better use 'late binding'.

ucan
09-13-2022, 12:08 PM
In an easy way what it will be ?

You will need to add a reference to the Scripting Runtime in the VBE for it to work.

ucan
09-13-2022, 12:09 PM
'late binding' ?

You'd better use 'late binding'.

georgiboy
09-13-2022, 12:18 PM
In an easy way what it will be ?

In the window where you paste the code:
Tools
References
Search for: Microsoft Scripting Runtime
Check the box