#128370 - 2004-10-25 03:18 PM
Reordering text file into CSV format
|
Anonymous
Anonymous
Unregistered
|
Hi I'm rediscovering Kix after a time in the wilderness. Here's wait I am trying to do and would appreciate any help.
I have a list of names and addresses in a list format.. e.g
Name Address Street City Phone number Fax
What I am tryig to do it remove the CRLF at the end of each line (which I have done using the fnStrip function.. I can concatenate a comma after each bit of info, but, what I am not sue how to do is.. count lines and put a LFCR after each fax number. Is this possible using Kix ?
i.e. find a string count on X characters then insert a CRLF
my code so far
Function fnSTRIP($sString,OPTIONAL $sDelimeter) If VarType($sString)<>8 Exit(1) EndIf If Not $sDelimeter $sDelimeter = Chr(13) + Chr(10) EndIf $sString = Split($sString,$sDelimeter) For Each $sString in $sString $fnSTRIP=$fnSTRIP+$sString Next EndFunction
Function insertline($a, $b, optional $c, optional $d) Dim $e,$f,$h,$x,$y If $b<0 Exit -3 EndIf If $c="" $d=1 EndIf $f="%temp%\~kixil00.tmp" ; temporary file to use Del $f If $d<>1 $d=0 EndIf $e=FreeFileHandle $insertline=-2 If $e=0 Exit -2 EndIf $insertline=Open($e,$a) If $insertline<>0 Exit @Error EndIf $h=FreeFileHandle If $h=0 $insertline=-2 $x=Close($e) Exit -2 EndIf $insertline=Open($h,$f,5) If $insertline<>0 $x=Close($e) Exit @Error EndIf If $b<>0 For $x=0 To $b-1 $y=ReadLine($e) If @Error<>0 $x=Close($e) $x=Close($h) Del $f $insertline=-3 Exit -3 Else $insertline=WriteLine($h,$y+@Crlf) EndIf Next EndIf $insertline=WriteLine($h,$c) $y=ReadLine($e) If @Error<>0 $x=Close($e) $x=Close($h) Del $f $insertline=-3 Exit -3 EndIf If $d=0 $x=WriteLine($h,$y+@Crlf) EndIf $y=ReadLine($e) While @Error=0 $insertline=Writeline($h,$y+@Crlf) $y=ReadLine($e) Loop $x=Close($e) $x=Close($h) Copy $f $a Del $f $insertline=0 Exit 0 EndFunction
$FileName = Dir("c:\temp\*.csv") While $FileName <> "" and @ERROR = 0 $fullpath = ("c:\temp\" + $filename)
IF Open(3, $fullpath) = 0 $x = ReadLine(3) WHILE @ERROR = 0 $y = WriteLine( 3 , ",")
;? "Line read: [" + $x + "]"
;Strip CRLF from document fnSTRIP($x)
$x = ReadLine(3) LOOP Close (3) ELSE BEEP ? "Config file not opened, error code: [" + @ERROR + "]" ENDIF
$FileName = Dir() ; retrieve next file loop
Kind Regards
Damon
|
Top
|
|
|
|
#128371 - 2004-10-25 03:50 PM
Re: Reordering text file into CSV format
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
You don't need to strip the CRLF - it's implied by ReadLine(), but is not actually present.
This sort of loop will catenate 5 lines:
Code:
$sIn=ReadLine($fdInputFile)
While Not @ERROR
$iLineCount=$iLineCount+1
$sOut=$sOut+',"'+$sIn+'"'
If Not ($iLineCount mod 5) SubStr($sOut,2)+@CRLF $sOut="" EndIf
$sIn=ReadLine($fdInputFile)
Loop
|
Top
|
|
|
|
#128372 - 2004-10-25 03:57 PM
Re: Reordering text file into CSV format
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Here is a fully functioning script to do it: Code:
$fdInputFile=FreeFileHandle() $sFilePath=".\demo.txt" If Open($fdInputFile,$sFilePath) "Cannot open file for input" Exit @ERROR EndIf $sIn=ReadLine($fdInputFile) While Not @ERROR $iLineCount=$iLineCount+1 $sOut=$sOut+',"'+$sIn+'"' If Not ($iLineCount mod 5) SubStr($sOut,2)+@CRLF $sOut="" EndIf $sIn=ReadLine($fdInputFile) Loop If $sOut <> "" SubStr($sOut,2)+@CRLF $sOut="" EndIf
|
Top
|
|
|
|
#128373 - 2004-10-25 04:37 PM
Re: Reordering text file into CSV format
|
Anonymous
Anonymous
Unregistered
|
Sorry for being thick.. but I am unable to get it to open the file for input I have change the filepath string but the code drops out with Cannot open file for inputScript error: failed to find/open script!
I do not have it open else where.....
thanks for your time/effort
|
Top
|
|
|
|
#128376 - 2004-10-25 08:49 PM
Re: Reordering text file into CSV format
|
Anonymous
Anonymous
Unregistered
|
Doing it from home, works a treat... weird... and thanks... not 100% what I am after... could you point me in the right direction to incorporate code that looks for the line with the fax number on it and at th end of that line inserts a LFCR ?
I've looked a Insertline() but it appears to work if you supply a line number... I dont know the line number as it could change.
Thanks again
Damon
|
Top
|
|
|
|
#128377 - 2004-10-26 10:09 AM
Re: Reordering text file into CSV format
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
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.
|
Top
|
|
|
|
#128378 - 2004-10-26 05:29 PM
Re: Reordering text file into CSV format
|
Anonymous
Anonymous
Unregistered
|
Hey Richard....
I'll try the code tonight... Here's exactly what I am trying to do..
<inhale.....hold....exhale>
I have a very long list of company names and address, similar to what you would use for a Mail merge.. but I want to import these into Outlook (2003). The required format is CSV. Your script neatly puts the document in CSV, but I need the code to put a CRLF at the end of the line that begins FAX, thats always the last line, but it isnt always the 7th (or 8th) line. Some companys have 5 lines in their address and some could have only 3
Hope that makes my task a little clearer and thank you for you patience
NB: I havent tried your latest script, will do tonight
Damon
|
Top
|
|
|
|
#128380 - 2004-10-26 06:11 PM
Re: Reordering text file into CSV format
|
Anonymous
Anonymous
Unregistered
|
Again... I'll give it a go tonight.... but it makes sense to me.. <Q jokes> BTW - could someone explain the mod expression as I couldnt find anything about it...
|
Top
|
|
|
|
#128381 - 2004-10-26 07:00 PM
Re: Reordering text file into CSV format
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
|
|
Top
|
|
|
|
#128382 - 2004-10-26 09:18 PM
Re: Reordering text file into CSV format
|
Anonymous
Anonymous
Unregistered
|
NTDOC...
ahhh got it... cheers
cheers...
|
Top
|
|
|
|
#128383 - 2004-10-26 11:28 PM
Re: Reordering text file into CSV format
|
Anonymous
Anonymous
Unregistered
|
Ok remoing the mod expression from richard code give me one continuous CSV style array..
Joels code then can not break up the single line into several line (all of which end in the fax number)
any ideas ?
|
Top
|
|
|
|
#128386 - 2004-10-27 12:50 PM
Re: Reordering text file into CSV format
|
Anonymous
Anonymous
Unregistered
|
Here's an exaple of the text
Code:
BROOK HOTELS The Coach House High Street Sevenoaks KentTN131HY Sarah Tunnadine
Tel: 0800 435165 Fax: 01732 741041
CHOICE HOTELS EUROPE Premier House 112 Station Road Edgware Middx HA8 7BJ Aphrodite Burton
Tel: 020 8233 2001 Fax: 020 8233 2075
CONCORDE HOTELS
Grosvenor Gardens House 35/37 Grosvenor Gardens London SW1W0BS Marketing Department
Tel: 0800 028 9881 Fax: 020 7630 0391
DEVERE HOTELS & LEISURE LTD 2100 Daresbury Park Daresbury, Warrington Cheshire WA4 4BP Alison Gray
Tel: 0870 240 0101 Fax: 01928 756 440
GRAND HERITAGE HOTELS Central Booking Agency 1st Floor Warwick House 181-183 Warwick Road London W14 8PU Greg McCreadv
Tel: 020 7244 6699 Fax: 020 7244 7799
GRANGE HOTELS 58 Rochester Row London SW1 1JU Jan Tel: 020 7233 7373 Fax: 020 7835 1888
IMPNEY HOTELS The Chateau Impney Hotel Droitwich Spa Worcs WR9 0BN Robert Hughes
Tel: 01905 774411 Fax: 01905 772371
INTER-CONTINENTAL & FORUM HOTELS Mayfair Inter-Continental Hotel Stratton Street London W1A 2AN Stephanie Segouta Tel: 020 7629 7777 Fax: 020 7409 7016
LAYTONA LEISURE GROUP Mendip Lodge Hotel Bath Road Frame Somerset BA11 2HP Stephen Crawley Tel: 01373 454211 Fax: 01373 471618
As you can see the line the begins 'FAX' is always the final line.. I wish to reorder the text so that each company and it's address/phone details are in a format that outlook can import, which is "company name","add1","addr2","City",County","post code","Phone","fax"
Damon
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 369 anonymous users online.
|
|
|