Link to home
Start Free TrialLog in
Avatar of krhag28
krhag28

asked on

How to fix orphan DBF

I want to remove the backlink to the DBC, in order to open the DBF, although it is orphan. The DBF is unknown, and it should be opened programatically, without user intervention.

There is no need to recreate the DBC, only to open the DBF. I have tried the code below, but not successful. Anybody that can give a solultion?

LPARAMETERS cFile
close databases all
cFiel = ForceExt(cFil, 'DBF')
nHnd = FOPEN(cFil, 2)

cStr = Fread(nHnd, 400)
nPos1 = 0
nPos2 = At('.dbc', cStr) + 2
jj = nPos2
for jj=nPos2 to 1 step -1
      cTkn = Substr(cStr, jj, 1)
      if cTkn = Chr(13)
            nPos1 = jj
      endif
next
for jj=nPos1 to nPos2
      =Fseek(nHnd, jj)
      =Fwrite(nHnd, Chr(0))
next
=Fseek(nHnd, 28)
=Fwrite(nHnd, Chr(0))
=Fclose(nHnd)
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

The code needs a small fix:
LPARAMETERS cFile

close databases all
cFil = ForceExt(cFile, 'DBF')
nHnd = FOPEN(cFil, 2)

cStr = Fread(nHnd, 400)
nPos1 = 0
nPos2 = At('.dbc', cStr) + 2
jj = nPos2
for jj=nPos2 to 1 step -1
      cTkn = Substr(cStr, jj, 1)
      if cTkn = Chr(13)
            nPos1 = jj
      endif
next
for jj=nPos1 to nPos2
      =Fseek(nHnd, jj)
      =Fwrite(nHnd, Chr(0))
next

*-- This code removes MEMO file and CDX indication
*=Fseek(nHnd, 28)
*=Fwrite(nHnd, Chr(0))

=Fclose(nHnd) 

Open in new window


BUT be careful!  If user selects non DBF file having DBF extension it will overwrite it...  Above code also does not work for tables having too many columns...

So it would need some additional tuning. I can provide better code later.
Avatar of krhag
krhag

I have tried it on a DBF which has CDX as well as DBC backlinks, and unfortunately the DBF seems to get corrupt, Foxpro reports Not a table...

I attach the table after manipulation with your code (including the last lines).

(txt extension because of upload restrictions)
Accounts.txt
ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your comments. However, what I need is code to remove references completely without user intervention - using hex editor is not an option in this case.
I will test the FREE TABLE command...
Free table seems to work fine :)  so it was more simple than expected.
Avatar of krhag28

ASKER

Simple solution, but nevertheless useful!
FREE TABLE should work but I'll post the updated code as I promised. The biggest problem of the code is it does not read the table structure but just the first 400 bytes so if you select any table having approx. 13 or more columns it must corrupt the table header...

The Hex editor was not the solution for your users but for you only.
Thanx.