Solved

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

Posted on 2003-10-23
1
257 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi : could not find program, '...exe' 2 193
Delphi selector screen 2 85
Convert MS Word document to a PDF file 9 92
shape, triangle, dbctrlgrid 3 17
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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