Kamis, 11 September 2008

Repair Table Dbf ( Visual Foxpro)

*--------------------------------
* cara penulisannya
* =DbRepair('c:\esoft\gltran.dbf')
*--------------------------------
FUNCTION DbRepair &&repairs in a table's head the number of records
LPARAMETERS fn &&full name with path and extension for .DBF file

* from FoxPro.h
#DEFINE MB_OK 0 && OK button only
#DEFINE MB_YESNO 4 && Yes and No buttons
#DEFINE MB_ICONQUESTION 32 && Warning query
#DEFINE MB_ICONEXCLAMATION 48 && Warning message
#DEFINE MB_DEFBUTTON1 0 && First button is default
#DEFINE IDYES 6 && Yes button pressed

LOCAL nf, nRecordsInHead, nHeadLength, nRecordLength

nf= FOPEN(fn,2)
IF nf<=0
=MESSAGEBOX("Can not open "+fn+"!",MB_OK+MB_ICONEXCLAMATION)
RETURN .F.
ENDIF

=FSEEK(nf,04,0)
nRecordsInHead= ASCN(FREAD(nf,4))

=FSEEK(nf,08,0)
nHeadLength= ASCN(FREAD(nf,2))

=FSEEK(nf,10,0)
nRecordLength= ASCN(FREAD(nf,2))

IF nHeadLength=0 .or. nRecordLength=0
=MESSAGEBOX(fn+" has a wrong structure!",MB_OK+MB_ICONEXCLAMATION)
=FCLOSE(nf)
RETURN .F.
ENDIF

nRecordsInFact= FSEEK(nf,0,2) &&file length
nRecordsInFact= int((nRecordsInFact-nHeadLength)/nRecordLength) &&real number of records

IF nRecordsInFact=nRecordsInHead &&it's Ok
=FCLOSE(nf)
RETURN .T.
ENDIF

IF IDYES#MESSAGEBOX(fn+" has:";
+CHR(13);
+CHR(13)+STR(nRecordsInHead)+" - number of records in the head";
+CHR(13)+STR(nRecordsInFact)+" - actual number of records";
+CHR(13)+STR(nRecordsInHead-nRecordsInFact)+" - records are lost";
+CHR(13);
+CHR(13)+"Do you want to repair this table?";
,MB_YESNO+MB_ICONQUESTION+MB_DEFBUTTON1)

=FCLOSE(nf)
RETURN .F.
ENDIF

=FSEEK(nf,04,0)
=FWRITE(nf,CHRN(nRecordsInFact,4),4) &&repair the head
=FCLOSE(nf)

IF !FILE(FORCEEXT(fn,"CDX"))
RETURN .T.
ENDIF

IF IDYES#MESSAGEBOX("Do you want to start REINDEX?";
,MB_YESNO+MB_ICONQUESTION+MB_DEFBUTTON1)
RETURN .T.
ENDIF

DEFINE WINDOW DbfRepairMsg FROM 08,10 to 40,89
ACTIVATE WINDOW DbfRepairMsg

SET CPDIALOG OFF
SET STATUS BAR OFF
SET TALK ON

SELECT 0
USE (fn) INDEX (FORCEEXT(fn,"CDX")) EXCLUSIVE
REINDEX
USE
SET TALK OFF
SET STATUS BAR ON

DEACTIVATE WINDOW DbfRepairMsg
RELEASE WINDOW DbfRepairMsg
RETURN .T.

*-------------------------------
FUNCTION ASCN &&converts binary string to numeric
LPARAMETERS s &&string, bytes go from tail to head

LOCAL i, n
n= 0
FOR i=LEN(s) TO 1 STEP -1
n=n*256+ASC(SUBSTR(s,i,1))
ENDFOR
RETURN n

*-------------------------------
FUNCTION CHRN &&converts numeric value to binary string,
&&bytes go from tail to head
LPARAMETERS n, ln &&the numeric value and output string length
LOCAL i, s, sc
s= ""
sc= n
FOR i=1 TO ln
s= s+CHR(sc%256)
sc= INT(sc/256)
ENDFOR
RETURN s

2 komentar:

Alex mengatakan...

Today to me came my relatives and asked about corrupted dbf files.I advised for their-repair dbf.To my great surprise utility helped for their very quickly.Moreover as they said application solved all problems in seconds and free of charge.They saw how tool analyzing data in damaged dbf files and associated files containing MEMO fields (fpt or dbt depending on the version).

database repair tool mengatakan...

Database recovery is one of our primary focuses. Look for a company that has realized the importance of database systems have for the clients. There are providers which already developed techniques for recovering databases that have proved to be highly successful.

Mengenai Saya

Foto saya
Bekasi, Jawa Barat, Indonesia
eSoft Software product : - Dental Clinic system - General Ledger (Accounting System) - Manufacturing System - Trading system