We help IT Professionals succeed at work.

How to pack all the paradox DBs of a directory?

SupWang
SupWang asked
on
Hi all,
I know how to pack one db. The function is defined as follows:
// Pack a Paradox or dBASE table
// The table must be opened execlusively before calling this function...
procedure PackTable(Table: TTable);

But How to pack all the paradox DBs of a directory by programming?

Regards, supwang
Comment
Watch Question

Commented:
Why not use a TFileListBox, then loop, changing the tablename property accordingly with the change in the item index.

Software Developer
CERTIFIED EXPERT
Commented:
Hello

  try this code, it will show all the db files in the specific directory, and then u can pack it file bye file

procedure TForm1.Button1Click(Sender: TObject);
var
 I: Integer;
 SearchRec: TSearchRec;
 Path :string;
begin
 Path := 'C:\DBFiles\';
 try
   I := FindFirst(Path + '*.db', 0, SearchRec);
   while I = 0 do
   begin
     listbox1.items.Add(SearchRec.Name);
     I := FindNext(SearchRec);
   end;
 except
   raise;
 end;
 for I := 0 to Listbox1.Items.Count -1 do
 begin
   Application.ProcessMessages;
   ListBox1.ItemIndex := I;
   PackTable(Path + ListBox1.Items[I]);
  end;
end;

Best regards
Mohammed Nasman

Author

Commented:
Thank you very much.

Best regards
supwang

Author

Commented:
Hi mnasman,
About this Line:
PackTable(Path + ListBox1.Items[I]);

But my procedure is:
procedure PackTable(Table: TTable);

Incompatible types:'String' and 'TTable'.  How to solve it?

Regards, supwang





Author

Commented:
Hi mnasman,
What's usage of this line?
ListBox1.ItemIndex := I;

Regards, supwang
Mohammed NasmanSoftware Developer
CERTIFIED EXPERT

Commented:
Hi SupWang

  for the second question, the line "ListBox1.ItemIndex := I;" it's set the selected item in the list box, so it's start with first item "it's the table name" then will change for the next one

  for the first question

  I pass the table name, not the dataset, so change ur procedure to get table name, or send me ur procedure at nasman@mogaza.org, and i will give you compelete sample :o)




Author

Commented:
Hi mnasman,

procedure PackTable(Table: TTable);
// Pack a Paradox or dBASE table
// The table must be opened execlusively before calling this function...
var
  Props: CURProps;
  hDb: hDBIDb;
  TableDesc: CRTblDesc;
 
begin
  // Make sure the table is open exclusively so we can get the db handle...
  if Table.Active = False then
    raise EDatabaseError.Create('Table must be opened to pack');
  if Table.Exclusive = False then
    raise EDatabaseError.Create('Table must be opened exclusively to pack');
 
  // Get the table properties to determine table type...
  Check(DbiGetCursorProps(Table.Handle, Props));
 
  // If the table is a Paradox table, you must call DbiDoRestructure...
  if Props.szTableType = szPARADOX then
  begin
    // Blank out the structure...
    FillChar(TableDesc, sizeof(TableDesc), 0);
    //  Get the database handle from the table's cursor handle...
    Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));
    // Put the table name in the table descriptor...
    StrPCopy(TableDesc.szTblName, Table.TableName);
    // Put the table type in the table descriptor...
    StrPCopy(TableDesc.szTblType, Props.szTableType);
    // Set the Pack option in the table descriptor to TRUE...
    TableDesc.bPack := True;
    // Close the table so the restructure can complete...
    Table.Close;
    // Call DbiDoRestructure...
    Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));
  end
  else
    // If the table is a dBASE table, simply call DbiPackTable...
    if Props.szTableType = szDBASE then
      Check(DbiPackTable(Table.DBHandle, Table.Handle, nil, szDBASE, TRUE))
    else
      // Pack only works on PAradox or dBASE; nothing else...
      raise EDatabaseError.Create('Table must be either of Paradox or dBASE ' +
               'type to pack');
 
  Table.Open;
end;




I change your code as following, is it right?
procedure PackDir(Directory: String);
var
  I: Integer;
  SearchRec: TSearchRec;
  Listbox: Tlistbox;
begin
  Listbox := Tlistbox.Create(Application);
  try
    I := FindFirst(Directory + '*.db', faAnyFile, SearchRec);
    while I = 0 do
    begin
      Listbox.items.Add(SearchRec.Name);
      I := FindNext(SearchRec);
    end;
    if Listbox.Items.Count <> 0 then
    begin
      for I := 0 to Listbox.Items.Count - 1 do
      begin
        Application.ProcessMessages;
        ListBox.ItemIndex := I;
        PackTable(Directory + ListBox.Items[I]);
      end;
    end;
  finally
    Listbox.Free;
    FindClose(SearchRec);
  end;
end;

Author

Commented:
Hi mnasman,
Can you don't modify the PackTable procedure? just change the PackDir procedure.

Thanks, supwang

Author

Commented:
Hi mnasman,
?
Mohammed NasmanSoftware Developer
CERTIFIED EXPERT

Commented:
Hello

 add table component to your form, and the procedure will look like this

procedure PackDir(Directory: String);
var
 I: Integer;
 SearchRec: TSearchRec;
 Listbox: Tlistbox;
begin
 Listbox := Tlistbox.Create(Application);
 ListBox.Parent := Form1;
 try
   I := FindFirst(Directory + '\*.db', faAnyFile, SearchRec);
   while I = 0 do
   begin
     Listbox.items.Add(SearchRec.Name);
     I := FindNext(SearchRec);
   end;
   if Listbox.Items.Count <> 0 then
   begin
     for I := 0 to Listbox.Items.Count - 1 do
     begin
       Application.ProcessMessages;
       ListBox.ItemIndex := I;
       Form1.Table1.close;
       Form1.Table1.TableName := Directory +'\' +ListBox.Items[I];
       Form1.Table1.Exclusive := true;
       Form1.Table1.open;
// now you will the table component instead of string
       PackTable(Form1.Table1);
     end;
   end;
 finally
   Listbox.Free;
   FindClose(SearchRec);
 end;
end;

Author

Commented:
Hi mnasman,
The code always show me "Table is busy."
My program like the Delphi demo(MastApp), using the TDatabase component. so I use the MastApp to do a test, have the "Table is busy" error too.
Could you please do a test?
I am using D3, The MastApp path is "C:\Program Files\Borland\Delphi 3\Demos\DB\MASTAPP".
I use this line to pack its db
------------------>
  PackDir('C:\Program Files\Borland\Delphi 3\Demos\Data');
------------------<

In D4, The MastApp path is "C:\Program Files\Borland\Delphi4\Demos\Db\Mastapp".

Best Regards, supwang

Author

Commented:
Hi mnasman,
Please help me. I will give more points to you.

Thanks, supwang

Author

Commented:
Hi mnasman,
I have already solved this problem. :-)

Best Regards, supwang

Explore More ContentExplore courses, solutions, and other research materials related to this topic.