Hi Tom,
Here's a rough VBA procedure that should do what's needed if you use it
to read the .DAT file and write to a .TXT file. It only processes one
line at a time, so the 120MB shouldn't be a problem.
Sub TestSubFixedWidth( _
SourceFile As String, _
DestinationFile As String)
'Searches a fixed width text file. If any record contains
'a CR that is not followed by a LF, replaces it with
'SUBST_CHAR.
Const SUBST_CHAR = "\" 'must be a single character.
Const REC_LENGTH = ??? 'Number of bytes in fixed-width record
'including CR+LF record separator.
Const DEBUG_FLAG = True 'Control whether debugging output is
'printed to the Immediate pane.
Dim S As String * REC_LENGTH 'Buffer for working on records.
Dim fIn As Long
Dim fOut As Long
Dim FilePos As Long 'only actually used for debugging output.
Dim oRE As Object 'VBScript_RegExp_55.RegExp
If DEBUG_FLAG Then
Debug.Print "--- " & SourceFile & " to " _
& DestinationFile & " ---"
End If
'Open input file
fIn = FreeFile()
Open SourceFile For Random As #fIn Len = REC_LENGTH
'Create output file
If Len(Dir(DestinationFile)) > 0 Then Kill DestinationFile
fOut = FreeFile()
Open DestinationFile For Random As #fOut Len = REC_LENGTH
'Create regular expression object
Set oRE = CreateObject("VBScript.Regexp")
With oRE
.Pattern = "\x0d([^\x0a])"
.Global = True
.Multiline = True
End With
Do Until EOF(fIn)
FilePos = Seek(fIn)
Get #fIn, , S 'read the next record
If DEBUG_FLAG Then
Debug.Print FilePos, Left(S, 10), Asc(Right(S, 2)) _
& " " & Asc(Right(S, 1))
'First item is record number, then the first 10 bytes
'of the record, and finally the ANSI values of the last
'two bytes. Except in the last record, these must be "13 10".
'If not, we have set the wrong record length.
End If
If Right(S, 2) <> vbCrLf Then 'either
' 1 we've set the wrong record length
' 2 it's not the kind of file we though it was
' 3 we're past the 0d0a that terminates the
' last record in the file.
'Whichever of these applies, there's no point continuing.
'(NB: it's conceivable that we might one day encounter
'a file in which the last record is not terminated. In
'that case this will have to be modified to check whether
'S contains an actual record.
Exit Do
End If
'Do the replacement and write to output file
S = oRE.Replace(S, SUBST_CHAR & "$1")
Put #fOut, , S
Loop
'Tidy up
Set oRE = Nothing
Close #fIn
Close #fOut
If DEBUG_FLAG Then
Debug.Print "-------------------------------------------"
End If
End Sub