Вы находитесь на странице: 1из 2

David Fung : Use Excel to repair DBF http://weblogs.foxite.com/davidfung/archive/2006/10/03/2577.


Welcome to Foxite.COM Community Weblog Sign in | Join | Help

David Fung

Winforms Grid Control

Fast and flexible Grid and TreeList easy to use, plugable rendering

Use Excel to repair DBF

My app has to deal with a lot of DBF which sometimes get corrupted. Upon checking, the
corruption is most likely due to some additional bytes after the logical end of the file. The
problem is VFP9 refuses to open it with error 2091:

Table "name" has become corrupted. The table will need to be repaired before using again.

It always puzzle me why VFP does not any built-in DBF repair command (not even the simple
one) although it has a native DBF engine.

Lately the frequency of corruption get to a point becomes annoying. So I look around the Net to
see if there is any DBF repair utility which I can incorporate into my app to fix the corruption
automatically when it is detected.

There are some but it is not apparent that they support command line execution or OLE
Automation, and they are not cheap. My client probably doesn't like absorbing the cost of it.

But my client has Excel installed in every workstation, and I noticed that Excel can open the
corrupted DBF okay without any problem. So I wrote a routine to use Excel to repair the DBF. It
simple asks Excel to open the corrupted DBF and save it again. It probably will not repair all
kinds of corruption, but at least it works in my case. Please see if it works for you as a DBF Repair

* ut_RepairDBF()
* Call this routine to repair a DBF file reported by VFP to be corrupted.
* tcFile = The full path to the DBF file to be repaired.
* e.g. 'c:\path\table.dbf'
* .t. if successful, .f. otherwise.
* The original DBF is backup with .bak extension.
* The repaired DBF has the same name as the original file.
* ut_RepairDBF('c:\path\table.dbf')
* Microsoft Excel is used to repair the DBF.
function ut_RepairDBF(tcFile)

local loExcel as Excel.Application

1 of 2 10/26/2010 5:36 AM
David Fung : Use Excel to repair DBF http://weblogs.foxite.com/davidfung/archive/2006/10/03/2577.aspx

local lcBackupFile
local lcFixedFile

if empty(tcFile) or not file(tcFile)

return .f.

loExcel = createobject("Excel.Application")
if vartype(loExcel)<>'O'
return .f.

lcBackupFile = alltrim(tcFile)+'.bak'
lcFixedFile = alltrim(tcFile)+'.tmp'

delete file (lcBackupFile)

delete file (lcFixedFile)

loExcel.ActiveWorkbook.SaveAs(lcFixedFile, xlDBF3)

rename (tcFile) to (lcBackupFile)

rename (lcFixedFile) to (tcFile)

Published Tuesday, October 03, 2006 4:55 PM by davidfung


Anonymous comments are disabled

© www.foxite.com

2 of 2 10/26/2010 5:36 AM