Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

backup a file 1

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
I am trying to back up a file using an open dialog and close dialog to specify the source and destination. The only part I really need is how to actually save it. Any ideas?

Here is my code:
Code:
procedure TfrmMain.cmdBackupClick(Sender: TObject);
var
  openDialog : TOpenDialog;    // Open dialog variable
  saveDialog : TSaveDialog;    // Save dialog variable
  OpenPath, SavePath : String; // Save file from and to
begin
  // Create the open dialog object - assign to our open dialog variable
  openDialog := TOpenDialog.Create(self);
  // Give the dialog a title
  openDialog.Title := 'Select the Quickbooks Company File to backup';
  // Set up the starting directory to be the current one
  openDialog.InitialDir := GetCurrentDir;
  // Only allow existing files to be selected
  openDialog.Options := [ofFileMustExist];
  // Allow only .dpr and .pas files to be selected
  openDialog.Filter :=
    'Quickbooks Company File|*.qbw';
  // Select pascal files as the starting filter type
  openDialog.FilterIndex := 1;
  // Display the open file dialog
  if openDialog.Execute
    then OpenPath := openDialog.FileName
    else
      begin
        ShowMessage('Backup cancelled');
        exit;
      end;
  // Free up the dialog
  openDialog.Free;
  //**************** --> Save File
  // Create the save dialog object - assign to our save dialog variable
  saveDialog := TSaveDialog.Create(self);
  // Give the dialog a title
  saveDialog.Title := 'Select where to save the Quickbooks Company File';
  // Set up the starting directory to be the current one
  saveDialog.InitialDir := GetCurrentDir;
  // Allow only .txt and .doc file types to be saved
  saveDialog.Filter := 'Quickbooks Company File|*.qbw';
  // Set the default extension
  saveDialog.DefaultExt := 'qbw';
  // Select text files as the starting filter type
  saveDialog.FilterIndex := 1;
  // Display the open file dialog
  if saveDialog.Execute
  then SavePath := saveDialog.FileName
    else
      begin
        ShowMessage('Backup cancelled');
        exit;
      end;
  // Free up the dialog
  saveDialog.Free;
end;




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Got it working.

Code:
procedure TfrmMain.cmdBackupClick(Sender: TObject);
var
  openDialog : TOpenDialog;    // Open dialog variable
  saveDialog : TSaveDialog;    // Save dialog variable
  FromFile, ToFile : File;     // Source and destination files
  NumRead, NumWritten: Integer;
  Buf: array[1..2048] of Char;
begin
  // Create the open dialog object - assign to our open dialog variable
  openDialog := TOpenDialog.Create(self);
  // Give the dialog a title
  openDialog.Title := 'Select the Quickbooks Company File to backup';
  // Set up the starting directory to be the current one
  openDialog.InitialDir := GetCurrentDir;
  // Only allow existing files to be selected
  openDialog.Options := [ofFileMustExist];
  // Allow only .dpr and .pas files to be selected
  openDialog.Filter :=
    'Quickbooks Company File|*.qbw';
  // Select pascal files as the starting filter type
  openDialog.FilterIndex := 1;
  // Display the open file dialog
  if openDialog.Execute then
    begin
      AssignFile(FromFile, openDialog.FileName);   {open output file}
      Reset(FromFile, 1);	{ Record size = 1 }
      openDialog.Free;  //free up the dialog

      //**************** --> Save File
      // Create the save dialog object - assign to our save dialog variable
      saveDialog := TSaveDialog.Create(self);
      // Give the dialog a title
      saveDialog.Title := 'Select where to save the Quickbooks Company File';
      // Set up the starting directory to be the current one
      saveDialog.InitialDir := GetCurrentDir;
      // Allow only .txt and .doc file types to be saved
      saveDialog.Filter := 'Quickbooks Company File|*.qbw';
      // Set the default extension
      saveDialog.DefaultExt := 'qbw';
      // Select text files as the starting filter type
      saveDialog.FilterIndex := 1;
      // Display the open file dialog
      if saveDialog.Execute then
        begin
          AssignFile(ToFile, saveDialog.FileName);
          Rewrite(ToFile, 1);	{ Record size = 1 }
          Canvas.TextOut(10, 10, 'Copying ' + IntToStr(FileSize(FromFile))
                          + ' bytes...');
          repeat
            BlockRead(FromFile, Buf, SizeOf(Buf), NumRead);
            BlockWrite(ToFile, Buf, NumRead, NumWritten);
          until (NumRead = 0) or (NumWritten <> NumRead);
          CloseFile(FromFile);
          CloseFile(ToFile);
        end
      else
        begin
          ShowMessage('Backup cancelled');
          exit;
        end;
      // Free up the dialog
      saveDialog.Free;
      MessageDlg('Backup Successful!', mtInformation, [mbOK],0);
    end
  else
    begin
      ShowMessage('Backup cancelled');
      exit;
    end;
end;




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
you can also use the CopyFile() function of windows :

Code:
CopyFile(Pchar(ExistingFileName),Pchar(NewFileName), False); // set flag true if you don't want to overwrite

cheers!

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top