Wiki Home

UDFRem Cdx

Namespace: VFP
Ever want to remove the CDX reference from a DBF without opening (USE'ing) the DBF?
This is particularly useful when you want to reindex the DBF because of index corruption (exiting VFP through abnormal termination) where you get an error every time you USE the table.

Basically, this does the same thing as would happen if you deleted the CDX, then opened the DBF and ignored the error "Structural index file not found".

FUNCTION RemCdx( pcFile )
LOCAL lnHandle, lnResult
nErr  = 0
lnResult = 0
if adir(laJnk,pcFile) = 0
   Return -1
lnFileSize = laJnk[2]

lnHandle = FOPEN(pcFile,12)   && Open file, R/W, Unbuffered
IF lnHandle > 0
  oErr = On("ERROR")
  ON ERROR nErr=Error()
  FSEEK(lnHandle,28,0) && Flags
  lcFlags = FREAD(lnHandle,1)
  * Remove flag for HAS CDX
  lcFlags = CHR( BITAND( ASC(lcFlags), BITNOT(0x01) ) )
  FSEEK(lnHandle,28,0) && Flags
  FWRITE( lnHandle, lcFlags )
  ON ERROR &oErr
  lnResult = iif(lnHandle=0,-1,lnHandle)
RETURN lnResult

This method will work reliably only for free tables - if your table is a part of DBC - then there will be records about indices in DBC (it can be easily fixed) AND it will be special mark about Primary Key index in main DBC record (for the table itself) that is not so easy to fix.
The easiest way to repair indices is to substitute corrupted/missed CDX file with the good one (from backup or from empty database build with gendbc script) - this cdx will be outdated or even empty, but its internal structure will be correct, so VFP REINDEX command will repair it.
-- Igor Korolyov
Contributors: wgcs
Category Code Samples Category UDF
( Topic last updated: 2005.06.06 05:40:25 AM )