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

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

aspx

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
winforms.pfgrid.com

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
alternative.

*------------------------------------------------------------------------
* ut_RepairDBF()
*
* Call this routine to repair a DBF file reported by VFP to be corrupted.
*
* PASSES:
* tcFile = The full path to the DBF file to be repaired.
* e.g. 'c:\path\table.dbf'
*
* RETURNS:
* .t. if successful, .f. otherwise.
*
* OUTPUT:
* The original DBF is backup with .bak extension.
* The repaired DBF has the same name as the original file.
*
* EXAMPLE:
* ut_RepairDBF('c:\path\table.dbf')
*
* REMARKS:
* 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.
endif

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

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

delete file (lcBackupFile)


delete file (lcFixedFile)

loExcel.Workbooks.Open(tcFile)
loExcel.ActiveWorkbook.SaveAs(lcFixedFile, xlDBF3)
loExcel.ActiveWindow.Close(.f.)
loExcel.Quit()

rename (tcFile) to (lcBackupFile)


rename (lcFixedFile) to (tcFile)
endfunc

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

Comments

Anonymous comments are disabled


© www.foxite.com

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