View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Checking for CR/LF in text file.

You have to read one character at a time and check that CR is followed by LF

Sub test()

Const LF = 10
Const CR = 13

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

Set readfile = fs.OpenTextFile("c:\temp\readfile.txt", _
ForReading, TristateFalse)
fs.CreateTextFile "c:\temp\writefile.txt"
Set writefile = fs.OpenTextFile("c:\temp\writefile.txt", _
ForWriting, TristateFalse)

CR_CHAR = False
Do While readfile.atendofstream = False
newchar = readfile.read(1)

Select Case Asc(newchar)

Case LF
If CR_CHAR = False Then
'case LF without CR
writefile.write Chr(CR)
Else
'reset status after CR then LF
CR_CHAR = False
End If
Case CR
CR_CHAR = True
Case Else
If CR_CHAR = True Then
'case CR without LF
writefile.write Chr(LF)
CR_CHAR = False
End If

End Select

writefile.write newchar
Loop

readfile.Close
writefile.Close


End Sub


"Erazmus" wrote:

Hi Guys,

Does anyone know how I would check that each line of a text file is
terminated with a CR/LF pair?

My macro needs to check the formatting of each line of a CSV file to
make sure it will load properly into a different application (This
part is nice and easy). Because each record/line in the file is
transactional in nature Excel is perfect for this but the one quirky
thing is that each line in the file must be terminated with a CR/LF
pair.

Any help will be appreciated.

I've tried working with something like this but each method reads the
line before termination? -

....
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("C:\TEST.TXT", ForReading, TristateTrue)
currentLine = f.ReadLine
....
and also
....
Open "C:\TEST.TXT" For Binary As #1
Line Input #1, LineofText
....

Cheers,
Deon Murtagh