Aug 7, 2008

Can you put a file in the desired location?

When you want to put a file in a certain location, you may want to check if it is possible to write to that location. You might not have write permission, the location might not exist or a file with the same name allready exists and cannot be overwritten because it is in use. With the code below, you can check this out:

* FUNCTION FILEOVERWRITE()

PARAMETERS cGo

fhandle =FCREATE(cGo)
IF fhandle > -1
=FCLOSE(fhandle)
DELE FILE (cGo)
RETURN .T.
ELSE
RETURN .F.
ENDIF

Aug 1, 2008

Function that returns a part of a comma-separated string

Whenever you need to to handle comma-separated string, you might want to check the function below, which makes use of VFP's ALINES() function to return a certain value in the string.


* Parameters: String (character) , which part (numeric), delimiter (character)
PARAMETERS cCsv, nPart, pdelimiter

IF PCOUNT() < 3
pDelimiter = ","
ENDIF

cCsv = STRTRAN(cCsv, pDelimiter, " "+CHR(13) )

ALINES(aCsv, cCsv , .T.)

* When array element 1 = .False. => return ""
* When the number of array elements < nPart => return ""
* Otherwise, return the piece of the string

RETURN IIF( TYPE( "aCsv[1]" ) = "L" , "", IIF( ALEN(aCsv,1) < nPart , "", aCsv[nPart] ) )