2 Attachment(s)
How to read text file by replacing a character as line break
I have a text file which I can import (read) if it is presented as separate lines. However sometimes I get the file with all the lines next to each other as if there is no line break. So the Line Read and While Not EOF command reads it as a single line whereas there may have been over 10,000 lines. I noticed that if I open this file in notepad it shows it as a single line without any breaks (but with a small box after each line which should have been a line break) but if I open it in TextPad it shows all the separate lines.
Now, since I may get both types of files, I need the application to be able to:
1. check if they are separate lines or a single line
2. break the Line Read text into separte lines and then process further.
I am attaching 2 separate attachments (withoutbreak.txt and withbreak.txt). Both these files look identical in TextPad but different if opened in notepad. A "*" is what should indicate end of line, so if you can guide me how to use it to indicate a line break and still continue with While Not EOF and Line Read to read the lines as separate lines, it'd be great!
Looking forward to your support!
Regards
Vinit
Re: How to read text file by replacing a character as line break
Try this for Withoutbreak.txt file:
Code:
Option Explicit
Private Sub Command1_Click()
Dim strTemp As String
Dim strContent() As String
'~~~ Read the entire line
Open "c:\withoutbreak.txt" For Input As #1
Line Input #1, strTemp
Close #1
strContent = Split(strTemp, vbLf) '~~~ Split the string
'~~~ Display the lines or do whatever you want
Dim i As Long
For i = LBound(strContent) To UBound(strContent)
Debug.Print strContent(i)
Next
End Sub
:wave:
Re: How to read text file by replacing a character as line break
as you do not show what code you are currently using....
vb Code:
open "somefile.txt" for input as 1
strfile = input(lof(1), #1) ' read entire file into string
close 1
if instr(strfile, vbnewline) = 0 then strfile = replace(strfile, vblf, vbnewline) ' if no new lines replace linefeed character with new line
' if you want the file contents in an array
arrfile = split(strfile, vbnewline)
Re: How to read text file by replacing a character as line break
The other way is to remove the vbCr rather than add the vbLf and then have to remove it with Split
Code:
Dim intFile As Integer
Dim strData As String
Dim strFile As String
Dim strRecords() As String
intFile = FreeFile
strFile = "C:\somefile.txt"
Open strFile For Input As intFile
strData = Input(LOF(intFile), intFile)
Close intFile
If InStr(strData, vbCrLf) <> 0 Then strData = Replace(strData, vbCr, "")
strRecords = Split(strData, vbLf)
It's as broad as it is long !
Re: How to read text file by replacing a character as line break
Replace() takes more time on large text. This should be quicker:
Code:
Open "C:\Temp\SomeFile.txt" for Input as #1
sData = Input(LOF(1), 1)
Close #1
If Instr(sData, vbCrLf) Then
sLines = Split(sData, vbCrLf)
Else
sLines = Split(sData, vbLf)
End If
Or
Code:
If Instr(sData, vbCrLf) Then sLineBreal = vbCrLf Else sLineBreak = vbLf
sLines = Split(sData, sLineBreak)