Quote:

not 100% what I am after




That's OK, we're 100% with you - we don't know what you are after either.

The code I've provided converts the flat file to a CSV format - all you have to do is write the output to a new file.

Here is the code updated to write the output to a new file. It does the following:
  • Reads input from file DEMO.TXT
  • Wraps each input line in quotes and converts embedded quotes in the data stream into the form that Excel likes.
  • Catenates every five lines into a single line with the fields delimited by commas
  • Writes the catenated line to DEMO.CSV, with CRLF appended.

Code:
 
$sInputFile=".\demo.txt"
$sOutputFile=".\demo.csv"

$fdInputFile=FreeFileHandle()
If Open($fdInputFile,$sInputFile)
"Cannot open file"+$sInputFile+"' for input"+@CRLF
"Reason: ["+@ERROR+"] "+@SERROR+@CRLF
Exit @ERROR
EndIf

If Exist($sOutputFile) Del $sOutputFile EndIf

$fdOutputFile=FreeFileHandle()
If Open($fdOutputFile,$sOutputFile,4+1)
"Cannot open file"+$sOutputFile+"' for output"+@CRLF
"Reason: ["+@ERROR+"] "+@SERROR+@CRLF
Exit @ERROR
EndIf

$sIn=ReadLine($fdInputFile)
While Not @ERROR
$iLineCount=$iLineCount+1
; Catenate line, fix speech marks to braindead MS csv style
$sOut=$sOut+',"'+Join(Split($sIn,'"'),'""')+'"'
If Not ($iLineCount mod 5) $=WriteLine($fdOutputFile,SubStr($sOut,2)+@CRLF) $sOut="" EndIf
$sIn=ReadLine($fdInputFile)
Loop

If $sOut <> "" $=WriteLine($fdOutputFile,SubStr($sOut,2)+@CRLF) $sOut="" EndIf

$=Close($fdInputFile)
$=Close($fdOutputFile)

Exit 0


If this doesn't fulfill your needs, take a deep breath, slow down and think about what you are trying to do. Then ask

You might like to outline the task to us - without context it is hard to determine if what we are suggesting will help or is even appropriate.

If you are having trouble working out what the code is doing, just ask.