Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

copy files form c:\ to drive a:\ with complete exception handling

Hi,
    Can you please help me. i am trying to make a program that copys a file
    or multiple files using the filelist box in drive a with a multiple select
   enabled with a complete exeception handling.. thanks.
0
vbeginner
Asked:
vbeginner
1 Solution
 
geobulCommented:
Hi,

Try this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, FileCtrl;

type
  TForm1 = class(TForm)
    flb: TFileListBox;
    btnCopy: TButton;
    procedure btnCopyClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const Dest = 'a:\';

function DiskInDrive(Drive: Char): Boolean;
var
  ErrorMode: word;
begin
  Drive := UpCase(Drive);
  if not (Drive in ['A'..'Z']) then
    raise EConvertError.Create('Not a valid drive ID');
  ErrorMode := SetErrorMode(SEM_FailCriticalErrors);
  try
    if DiskSize(Ord(Drive) - $40) = -1 then
      DiskInDrive := False
    else
      DiskInDrive := True;
  finally
    SetErrorMode(ErrorMode);
  end;
end;

function CopyAFile(const fname, FromFolder, ToFolder: string): boolean;
var
  res: integer;
  newname: string;
begin
  result := true;
  try
    if FileExists(ToFolder + fname) then begin
      res := windows.MessageBox(0, PChar('File '+fname+' exists. Overwrite?'), PChar('Question'), MB_YESNOCANCEL or MB_ICONQUESTION);
      if res = IDYES then begin // overrite
        try
          if not CopyFile(PChar(FromFolder + fname), PChar(ToFolder + fname), false) then begin
            windows.MessageBox(0, PChar('Error copying file '+fname), PChar('Error'), MB_OK or MB_ICONERROR);
            result := false;
            exit;
          end;
        except
          windows.MessageBox(0, PChar('Error copying file '+fname), PChar('Error'), MB_OK or MB_ICONERROR);
          result := false;
          exit;
        end;
      end;
      if res = IDNO then begin // ask for rename
        repeat
          newname := InputBox('Rename '+fname, 'Enter new name:',fname);
          if FileExists(ToFolder + newname) then begin
            if windows.MessageBox(0, PChar('File '+fname+' exists. Enter new name?'), PChar('Question'), MB_YESNO or MB_ICONQUESTION) = IDNO then begin
              ShowMessage('File '+fname+' wan''t coppied');
              exit;
            end;
          end;
        until true;
        try
          if not CopyFile(PChar(FromFolder + fname), PChar(ToFolder + newname), false) then begin
            windows.MessageBox(0, PChar('Error copying file '+fname), PChar('Error'), MB_OK or MB_ICONERROR);
            result := false;
            exit;
          end;
        except
          windows.MessageBox(0, PChar('Error copying file '+fname), PChar('Error'), MB_OK or MB_ICONERROR);
          result := false;
          exit;
        end;
      end;
      if res = IDCANCEL then begin // copy cancelled
        ShowMessage('File '+fname+' wan''t coppied');
        exit;
      end;
    end else begin // normal copy
      try
        if not CopyFile(PChar(FromFolder + fname), PChar(ToFolder + fname), false) then begin
          windows.MessageBox(0, PChar('Error copying file '+fname), PChar('Error'), MB_OK or MB_ICONERROR);
          result := false;
          exit;
        end;
      except
        windows.MessageBox(0, PChar('Error copying file '+fname), PChar('Error'), MB_OK or MB_ICONERROR);
        result := false;
        exit;
      end;
    end;
  except
    result := false;
  end;
end;

procedure TForm1.btnCopyClick(Sender: TObject);
var
  i: integer;
begin
  with flb do begin
    if SelCount > 0 then begin
      if DiskInDrive('a') then begin
        for i := 0 to Items.Count - 1 do begin
          if Selected[i] then begin
            if not CopyAFile(flb.Items[i], flb.Directory + '\', Dest) then begin
              ShowMessage('Copy failed !');
              exit;
            end;
          end;
        end;
        ShowMessage('Done');
      end else begin
        ShowMessage('Enter a disk in drive A: and try again');
      end;
    end;
  end;
end;

end.

Regards, Geo
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now