Solved

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

Posted on 2003-10-23
1
256 Views
Last Modified: 2010-04-05
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
Comment
Question by:vbeginner
1 Comment
 
LVL 17

Accepted Solution

by:
geobul earned 250 total points
ID: 9612891
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question