>>Hi
>>
>>I have a table which has crashed when the server powered off - I can see the dara on the file when I view with a text editor but when I open in VFP6 it reports 0 records
>>
>>Any help would be very welcome
>>
>>Colin
>
>Colin,
>You can try this after a backup:
>
>
>* RepairHeaderAndMemo.prg
>* Parameters
>* tcDBF : Full path and name of dbf file to repair
>* Remarks :
>* Has CDX flag is cleared
>Lparameters tcDBF
>Local handle, lnFileSize, lnReccount, lnHeaderSize, lnRecordSize, ;
> lnCalculatedReccount, llHasMemo
>Set Step On
>handle=fopen(tcDBF,12)
>lnFileSize = fseek(handle,0,2)
>* Read header info
>lnReccount = ReadBytes(handle, 4,4)
>lnHeaderSize = ReadBytes(handle, 8,2)
>lnRecordSize = ReadBytes(handle,10,2)
>
>lnCalculatedReccount = floor((lnFileSize-lnHeaderSize)/lnRecordSize)
>If lnCalculatedReccount # lnReccount
> WriteBytes(handle, 4,4,lnCalculatedReccount)
>Endif
>WriteBytes(handle, 28,1,0)
>llHasMemo=file(forceext(tcDBF,'FPT'))
>If llHasMemo
> RepairMemo(forceext(tcDBF,'FPT'))
> WriteBytes(handle, 28,1,0x02)
>Endif
>=fclose(handle)
>
>Function RepairMemoHeader
>Lparameters tcMemoFilename
>Local handle, lnFileSize, lnNextBlockPointer, lnBlockSize, lnFirstBlock, lnCalculatedFileSize
>
>handle=fopen(tcMemoFilename,12)
>lnFileSize = fseek(handle,0,2)
>* Read header info
>lnNextBlockPointer = ReadBytes(handle, 0,4,.T.)
>lnBlockSize = ReadBytes(handle, 6,2,.T.)
>
>* Specific to me - no blocksize setting to something other than default 0x40
>If lnBlockSize # 0x40
> WriteBytes(handle, 6,2,0x40,.T.)
> lnBlockSize=0x40
>Endif
>*
>
>lnFirstBlock = ceiling(512/lnBlockSize)
>lnCalculatedFileSize = lnNextBlockPointer*lnBlockSize
>
>* Fix if needs repair
>If !(lnFileSize >= 512 ;
> and lnNextBlockPointer >= lnFirstBlock ;
> and lnCalculatedFileSize >= lnFileSize)
> lnNextBlockPointer = max(lnNextBlockPointer, lnFirstBlock)
> lnFileSize = lnNextBlockPointer * lnBlockSize
> WriteBytes(handle, 0,4,lnNextBlockPointer,.T.)
> =fchsize(handle, lnFileSize)
>Endif
>Endwith
>=fclose(handle)
>
>Function ReadBytes
>Lparameters tnHandle, tnPos, tnSize, tlLR
>Local lcString, lnRetValue,ix
>=fseek(tnHandle, tnPos,0)
>lcString = fread(tnHandle, tnSize)
>lnRetValue = 0
>For ix=0 to tnSize-1
> lnRetValue = lnRetValue + asc(substr(lcString,ix+1)) * ;
> iif(tlLR,256^(tnSize-1-ix),256^ix)
>Endfor
>Return int(lnRetValue)
>
>Function WriteBytes
>Lparameters tnHandle, tnPos, tnSize, tnNumber, tlLR
>Local lcString, lnLowDword, lnHighDword,ix
>lcString=''
>If tlLR
> For ix=tnSize-1 to 0 step -1
> lcString=lcString+chr(tnNumber/256^ix%256)
> Endfor
>Else
> For ix=0 to tnSize-1
> lcString=lcString+chr(tnNumber/256^ix%256)
> Endfor
>Endif
>=fseek(tnHandle, tnPos,0)
>Return fwrite(tnHandle,lcString)
>
Cetin
Cetin
Thanks
Couple of questions
1 I have inserted the full path of the table as you can see - brings up a program error - what have I done wrong?
2. When I compile the program I get an endwith error - does this matter?
Colin
* RepairHeaderAndMemo.prg
* Parameters
* tcDBF : Full path and name of dbf file to repair
* Remarks :
* Has CDX flag is cleared && New copy has no CDX
Lparameters tcDBF
tcDBF="c:\globalnew.dbf"
Local handle, lnFileSize, lnReccount, lnHeaderSize, lnRecordSize, ;
lnCalculatedReccount, llHasMemo
Set Step On
handle=fopen(tcDBF,12) && Opened readwrite
lnFileSize = fseek(handle,0,2) && Get file size
* Read header info
lnReccount = ReadBytes(handle, 4,4)
lnHeaderSize = ReadBytes(handle, 8,2)
lnRecordSize = ReadBytes(handle,10,2)
lnCalculatedReccount = floor((lnFileSize-lnHeaderSize)/lnRecordSize)
If lnCalculatedReccount # lnReccount && If calculated # recorded fix it
WriteBytes(handle, 4,4,lnCalculatedReccount)
Endif
WriteBytes(handle, 28,1,0) && Clear has CDX, has memo, isDBC flags
llHasMemo=file(forceext(tcDBF,'FPT'))
If llHasMemo
RepairMemo(forceext(tcDBF,'FPT'))
WriteBytes(handle, 28,1,0x02) && Set has memo flag
Endif
=fclose(handle)
Function RepairMemoHeader
Lparameters tcMemoFilename
Local handle, lnFileSize, lnNextBlockPointer, lnBlockSize, lnFirstBlock, lnCalculatedFileSize
handle=fopen(tcMemoFilename,12) && Opened readwrite
lnFileSize = fseek(handle,0,2) && Get file size
* Read header info
lnNextBlockPointer = ReadBytes(handle, 0,4,.T.) && Stored in left-to-right format
lnBlockSize = ReadBytes(handle, 6,2,.T.) && Stored in left-to-right format
* Specific to me - no blocksize setting to something other than default 0x40
If lnBlockSize # 0x40
WriteBytes(handle, 6,2,0x40,.T.)
lnBlockSize=0x40
Endif
*
lnFirstBlock = ceiling(512/lnBlockSize) && Possible min lnNextblockpointer
lnCalculatedFileSize = lnNextBlockPointer*lnBlockSize
* Fix if needs repair
If !(lnFileSize >= 512 ;
and lnNextBlockPointer >= lnFirstBlock ;
and lnCalculatedFileSize >= lnFileSize) && Memo needs repair
lnNextBlockPointer = max(lnNextBlockPointer, lnFirstBlock)
lnFileSize = lnNextBlockPointer * lnBlockSize
WriteBytes(handle, 0,4,lnNextBlockPointer,.T.) && Fix next block pointer
=fchsize(handle, lnFileSize) && Fix filesize
Endif
Endwith
=fclose(handle)
Function ReadBytes
Lparameters tnHandle, tnPos, tnSize, tlLR
Local lcString, lnRetValue,ix
=fseek(tnHandle, tnPos,0) && Go to pos
lcString = fread(tnHandle, tnSize) && Read tnSize bytes
lnRetValue = 0
For ix=0 to tnSize-1 && Convert to a number
lnRetValue = lnRetValue + asc(substr(lcString,ix+1)) * ;
iif(tlLR,256^(tnSize-1-ix),256^ix)
Endfor
Return int(lnRetValue)
Function WriteBytes
Lparameters tnHandle, tnPos, tnSize, tnNumber, tlLR
Local lcString, lnLowDword, lnHighDword,ix
lcString=''
If tlLR
For ix=tnSize-1 to 0 step -1
lcString=lcString+chr(tnNumber/256^ix%256)
Endfor
Else
For ix=0 to tnSize-1
lcString=lcString+chr(tnNumber/256^ix%256)
Endfor
Endif
=fseek(tnHandle, tnPos,0) && Go to pos
Return fwrite(tnHandle,lcString)
Specialist in Advertising, Marketing, especially Direct Marketing
I run courses in Business Management and Marketing