* Check DBF files for potential corruption and attempt to fix most basic problems * such as incorrect record count * * Pass the file skeleton for the dbf files to be checked. * LPARA cTargetFile SET TALK OFF LOCAL LGotError, cdbffile, cDiscard, nHdrRecCount, nBeginData, nRecSize, nRecsFound, ; l1replace, cFixedRecC, nFileNum, l1loop, hnddbffile, nFileSize, nFileCount, cfileskel STORE '.\*.dbf' TO cfileskel IF PARA()>0 AND TYPE("cTargetFile")="C" AND FILE(cTargetFile) STORE cTargetFile TO cfileskel ENDIF DIMENSION aDbfs(1) STORE .F. TO LGotError ? 'Checking Tables:' FOR nFileNum=1 TO ADIR(aDbfs, cfileskel ) IF aDbfs[nFileNum,2] = 0 STORE .T. TO LGotError ? aDbfs[nFileNum,1] ?? ' 0 bytes (you must manually delete or restore)' LOOP ENDIF STORE FOPEN(aDbfs[nFileNum,1],2) TO hnddbffile STORE FSEEK(hnddbffile, 0, 2) TO nFileSize FSEEK(hnddbffile, 4, 0) && skip past file type byte and 3 bytes for modified date STORE readx(hnddbffile,4) TO nHdrRecCount STORE readx(hnddbffile,2) TO nBeginData STORE readx(hnddbffile,2) TO nRecSize STORE (nFileSize-(nBeginData+1))/nRecSize TO nRecsFound STORE IIF(nFileSize=nBeginData,0,nRecsFound) TO nRecsFound IF nRecsFound # nHdrRecCount STORE .T. TO LGotError ? aDbfs[nFileNum,1] ?? ' ' ?? nRecsFound ?? ' physical records, but ' + ALLT(STR(nHdrRecCount)) + ' record count. ' ?? ' -> ' STORE '' TO cFixedRecC FOR l1loop=1 TO 4 STORE CHR(dec(SUBSTR(PADL(hex(nRecsFound),8,'0'),l1loop*2-1,2)))+cFixedRecC TO cFixedRecC ENDFOR STORE FSEEK(hnddbffile, 4, 0) TO zen && reposition back to num records in file ?? IIF(FWRITE(hnddbffile,cFixedRecC)=LEN(cFixedRecC), " Wrote. ", " Write failed. ") STORE nBeginData + 1 + ( INT(nRecsFound)*nRecSize ) TO camoflag ?? IIF(FCHSIZE(hnddbffile,camoflag)=camoflag, " Resized.", " Resize Failed.") ENDIF FCLOSE(hnddbffile) ENDFOR ? IIF(LGotError,'Errors Found!','All Tables appear okay') ? RETURN *-------------------------------------------------------------------- FUNCTION readx *-------------------------------------------------------------------- PARAMETER hndFile, l1len PRIVATE l1str, l1tmp, l1value, l1loop STORE .F. TO l1str, l1tmp, l1value, l1loop STORE FREAD(hndFile,l1len) TO l1str STORE '' TO l1tmp FOR l1loop=1 TO l1len STORE RIGHT('00'+hex(ASC(SUBSTR(l1str,l1loop,1))),2)+l1tmp TO l1tmp ENDFOR STORE dec(l1tmp) TO l1value RETURN l1value endfunc *-------------------------------------------------------------------- FUNCTION hex *-------------------------------------------------------------------- PARAMETERS l1int PRIVATE l1part, l1loop, l1hex, l1str STORE .F. TO l1part, l1loop,l1hex,l1str STORE '0123456789ABCDEF' TO l1str STORE '' TO l1hex FOR l1loop=7 TO 0 STEP -1 STORE INT(l1int/(16^l1loop)) TO l1part STORE l1hex+SUBSTR(l1str,l1part+1,1) TO l1hex STORE l1int-l1part*(16^l1loop) TO l1int ENDFOR DO WHILE LEFT(l1hex,1)='0'.AND.LEN(l1hex)>1 STORE SUBSTR(l1hex,2) TO l1hex ENDDO RETURN l1hex endfunc *-------------------------------------------------------------------- FUNCTION dec *-------------------------------------------------------------------- PARAMETERS l1hex PRIVATE l1loop, l1int, l1str, l1ctr STORE '0123456789ABCDEF' TO l1str STORE 0 TO l1int FOR l1loop=LEN(l1hex)-1 TO 0 STEP -1 STORE l1int+(16^l1loop)*(AT(SUBSTR(l1hex,LEN(l1hex)-l1loop,1),l1str)-1) TO l1int ENDFOR RETURN l1int endfunc