Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem CSV File
Message
 
 
To
12/10/2007 20:16:52
Neil Mc Donald
Cencom Systems P/L
The Sun, Australia
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01258459
Message ID:
01260783
Views:
22
Hi Neil,

Fist, your code will eliminate all CR+LF even from memo field. Was it your intention?
You can shorten code by using an array instead of numbered variables.
...
DIMENSION laVal[FCOUNT("curTable ")]
...
		laVal[lnElement] = lcValue
		lnElement=lnElement+1
	ENDDO
	INSERT INTO curTable (f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11) ;
		VALUES (laVal[1],laVal[2],laVal[3],laVal[4],laVal[5], ;
				laVal[6],laVal[7],laVal[8],laVal[9],laVal[10],laVal[11])
ENDDO
>
>Thanks for your prior help with this problem, the solution ended up requiring another approach.
>
>The main problems were commas & CR LF pairs in the character elements as well as CR LF pairs in the numeric elements.
>
>For anyone that has a similar problem I have listed the code below, also any suggestions to improve the code would be welcomed.
>
>
>Local lcChk, lcFileToImport, lcString, lcValue
>LOCAL lnBlockSize, lnCurPos, lnElement, lnNoElements, lnFH, lnFinPos, lnSize
>
>lcFileToImport = Getfile("", "Select File to Import")
>If Len(lcFileToImport) = 0
>	Return
>Endif
>
>lnFH = Fopen(lcFileToImport, 2)
>If lnFH < 0
>	Messagebox("Cannot open '" + lcFileToImport + "'", 0, "Cannot fix")
>	Return
>ENDIF
>
>CREATE CURSOR curTable (f1 C(5),f2 M,f3 C(8),f4 C(5),f5 C(3),f6 C(3),f7 C(3),f8 C(5),f9 C(8),f10 C(5),f11 C(8))
>
>lnBlockSize = 2048 & set read block size
>
>DO WHILE !FEOF(lnFH)
>	lcString = FGETS(lnFH, lnBlockSize)
>	lnCurPos=1
>	lnSize=1
>	lnNoElements=11
>	lnElement = 1
>	DO WHILE lnElement <= lnNoElements
>		lcChk=SUBSTR(lcString,lnCurPos,lnSize)
>		IF lcChk=CHR(34) && Is alpha data so look for ending "
>			lcString=SUBSTR(lcString,lnCurPos+1)
>			lnFinPos=AT(CHR(34),lcString)
>		ELSE
>			lnFinPos=AT(CHR(44),lcString)
>		ENDIF
>		DO CASE
>			CASE lnFinPos>lnCurPos+1 && Has data
>				lcValue=SUBSTR(lcString,lnCurPos,lnFinPos-1)
>			CASE lnFinPos=lnCurPos && No Data
>				lcValue=""
>			CASE lnFinPos = 0
>				DO WHILE lnFinPos = 0 && Must be a CRLF pair within the String
>					lcString = ALLTRIM(lcString) + FGETS(lnFH,lnBlockSize)
>					IF lcChk=CHR(34)
>					lnFinPos=AT(CHR(34),lcString)
>					lcValue=SUBSTR(lcString,lnCurPos,lnFinPos-1)
>					ELSE
>					lnFinPos=AT(CHR(44),lcString)
>					lcValue=SUBSTR(lcString,lnCurPos,lnFinPos-1)
>					ENDIF
>				ENDDO
>		ENDCASE
>			IF lcChk=CHR(34)
>			lcString=SUBSTR(lcString,lnFinPos+2)
>			ELSE
>			lcString=SUBSTR(lcString,lnFinPos+1)
>			ENDIF
>		DO CASE
>			CASE lnElement = 1
>				lcf1 = lcValue
>			CASE lnElement = 2
>				lcf2 = lcValue
>			CASE lnElement = 3
>				lcf3 = lcValue
>			CASE lnElement = 4
>				lcf4 = lcValue
>			CASE lnElement = 5
>				lcf5 = lcValue
>			CASE lnElement = 6
>				lcf6 = lcValue
>			CASE lnElement = 7
>				lcf7 = lcValue
>			CASE lnElement = 8
>				lcf8 = lcValue
>			CASE lnElement = 9
>				lcf9 = lcValue
>			CASE lnElement = 10
>				lcf10 = lcValue
>			CASE lnElement = 11
>				lcf11 = lcValue
>		ENDCASE
>		lnElement=lnElement+1
>	ENDDO
>	INSERT INTO curTable (f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11) ;
>		VALUES (lcf1,lcf2,lcf3,lcf4,lcf5,lcf6,lcf7,lcf8,lcf9,lcf10,lcf11)
>ENDDO
>
>BROWSE
>
>Fclose(lnFH)
>Close Tables All
>Release All
>
>
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform