Read text file and UNIX end of line

S

swellfr

Hi,
I would like to know if there is a clean way to read files created
under unix ( so terminating by LF instead if CRLF on Windows) in VBA.
It seems that when i call "Line Input" , i get the full file at once .
Would it be possible to get it line by line?

Thx
 
J

Joel

Yes you can. The code below converts unix to dos fixing the Cr and Linefeed
so they are dos format.

Unix files only need a carriage return, dos uses both Carriagge return and
linefeed.

If you just want to modify the code below as required. To read the UNIX
file you need to open the file in binary mode andread one character at a
time. the end of the line would be when yo uget to the CR.





Sub fixcrlf()

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const MyPath = "C:\temp\"
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Dim CR As String
Dim LF As String

CR = Chr(13)
LF = Chr(10)


Set fsread = CreateObject("Scripting.FileSystemObject")
Set fswrite = CreateObject("Scripting.FileSystemObject")

ReadFileName = "intext.txt"
WriteFileName = "outtext.txt"


'open files
ReadPathName = MyPath + ReadFileName
Set fread = fsread.GetFile(ReadPathName)
Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault)

WritePathName = MyPath + WriteFileName
fswrite.CreateTextFile WritePathName
Set fwrite = fswrite.GetFile(WritePathName)
Set tswrite = fwrite.OpenAsTextStream(ForWriting, TristateUseDefault)

OutputLine = ""
FoundCR = False
Do While tsread.atendofstream = False

MyChar = tsread.Read(1)

Select Case MyChar

Case LF
If FoundCR = False Then
tswrite.write CR
Else
FoundCR = False
End If
Case CR
If FoundCR = True Then
tswrite.write LF
Else
FoundCR = True
End If
Case Else
If FoundCR = True Then
tswrite.write LF
FoundCR = False
End If
End Select
tswrite.write MyChar
Loop

tswrite.Close
tsread.Close

End Sub
 
J

Joel

Yes you can. The code below converts unix to dos fixing the Cr and Linefeed
so they are dos format.

Unix files only need a carriage return, dos uses both Carriagge return and
linefeed.

If you just want to modify the code below as required. To read the UNIX
file you need to open the file in binary mode andread one character at a
time. the end of the line would be when yo uget to the CR.





Sub fixcrlf()

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const MyPath = "C:\temp\"
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Dim CR As String
Dim LF As String

CR = Chr(13)
LF = Chr(10)


Set fsread = CreateObject("Scripting.FileSystemObject")
Set fswrite = CreateObject("Scripting.FileSystemObject")

ReadFileName = "intext.txt"
WriteFileName = "outtext.txt"


'open files
ReadPathName = MyPath + ReadFileName
Set fread = fsread.GetFile(ReadPathName)
Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault)

WritePathName = MyPath + WriteFileName
fswrite.CreateTextFile WritePathName
Set fwrite = fswrite.GetFile(WritePathName)
Set tswrite = fwrite.OpenAsTextStream(ForWriting, TristateUseDefault)

OutputLine = ""
FoundCR = False
Do While tsread.atendofstream = False

MyChar = tsread.Read(1)

Select Case MyChar

Case LF
If FoundCR = False Then
tswrite.write CR
Else
FoundCR = False
End If
Case CR
If FoundCR = True Then
tswrite.write LF
Else
FoundCR = True
End If
Case Else
If FoundCR = True Then
tswrite.write LF
FoundCR = False
End If
End Select
tswrite.write MyChar
Loop

tswrite.Close
tsread.Close

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top