Solved

API -> CopyFile - DeleteFile - RenameFile ( functions )

Posted on 2000-03-02
11
837 Views
Last Modified: 2012-06-27

i use this functions in my program, and i'm wondering if theese functions is enough good or is better available?

Esk
0
Comment
Question by:esk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 5

Expert Comment

by:TheNeil
ID: 2577255
Do they work? Why mess with it if they don't. There are various ways to copy files in Delphi (using streams for instance) and they might make the calls a little simpler but the standard calls have always done what they're supposed to do so why change them

The Neil =:)
0
 
LVL 17

Expert Comment

by:inthe
ID: 2577277
I agree with TheNeil, you could use the shellapi stuff with fo_copy,fo_rename etc or copyfileex etc(nt) but why complicate things..
0
 

Author Comment

by:esk
ID: 2578056
Do you have any function?

Esk
0
Technology Partners: 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!

 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 2578183
well instead of deletefile could use this to delete to recycle bin:

uses
ShellApi;
 
procedure DeleteToBin;
var
  T:TSHFileOpStruct;
  X: Integer;
begin
    with T do
    begin
      Wnd:=0;
      wFunc:=FO_DELETE;
      pFrom:='C:\Hello.bmp';        
      fFlags:={FOF_FILESONLY or}  FOF_ALLOWUNDO or FOF_SILENT or  
FOF_NOCONFIRMATION;
    end;
    SHFileOperation(T);
end;


to copy a single file or whole dir of files can do:

uses
  ShellAPI, ShlObj, ActiveX;
 
procedure TForm1.Button1Click(Sender: TObject);
  CONST
      NameCount = 200;
    VAR
      SHFileOpStruct:  TSHFileOpStruct;
      zFromDirectory:  ARRAY[0..NameCount-1] OF CHAR;
      zToDirectory  :  ARRAY[0..NameCount-1] OF CHAR;
 
  BEGIN
  try
    SHGetMalloc(pMalloc);
ZeroMemory(@zFromDirectory, NameCount);
ZeroMemory(@zToDirectory,  NameCount);
StrPCopy (zFromDirectory,'C:\Windows\*.*');
StrPCopy(zToDirectory,'C:\Temp');
  WITH SHFileOpStruct DO
    BEGIN
    Wnd  := Handle;
    wFunc := FO_COPY;
    pFrom := @zFromDirectory;
    pTo  := @zToDirectory;
    fFlags := FOF_NoConfirmation OR FOF_RenameOnCollision;
    END;
    SHFileOperation(SHFileOpStruct);
    finally
    pMalloc._Release;
    pMalloc := nil;
    end;



and of course can use fo_move to move the file/files...


using CopyFileEx():

type
   TProgressForm = class(TForm)
     AbortButton: TButton;
     ProgressBar: TProgressBar;
     procedure AbortButtonClick(Sender: TObject);
   private
     { Private declarations }
   public
     { Public declarations }
     FCancel: BOOL;
   end;
   // form has fsStayOnTop formstyle!
   
 implementation
   
 {$R *.DFM}
   
 procedure TProgressForm.AbortButtonClick(Sender: TObject);
 begin
   FCancel := True;
 end;
   
 // Note: could use int64 instead of COMP, but that would make this D4 specific
 
Function CopyCallback( TotalFileSize, TotalBytesTransferred,
                       StreamSize, StreamBytesTransferred: COMP;                      
 dwStreamNumber, dwCallbackReason : DWORD;                       hSourceFile,
 hDestinationFile: THandle;
                       progressform: TProgressForm ): DWORD; stdcall; Var
   newpos: Integer;
 Begin
   Result := PROCESS_CONTINUE;
   If dwCallbackReason = CALLBACK_CHUNK_FINISHED Then Begin
     newpos := Round( TotalBytesTransferred / TotalFileSize * 100 );     With
 progressform.Progressbar Do
       if newpos <> Position then
         Position := newpos;
     Application.ProcessMessages;    
   End; { If }
 End; { CopyCallback }
   
 Function DoFilecopy( Const source, target: String ): Boolean;
 Var
   progressform: TProgressForm;
 Begin
   progressform := TProgressform.Create;
   try
     progressform.Show;
     Application.ProcessMessages;
     Result := CopyFileEx(
                 PChar( source ),
                 PChar( target ),
                 @CopyCallback,
                 Pointer( progressform ),
                 @progressform.FCancel,
                 0 );
 finally
   progressform.Hide;
   progressform.free;
 end;
 end; { DoFileCopy }


0
 

Author Comment

by:esk
ID: 2578434
progressform: TProgressForm;

undeclared intentifier

Esk
0
 

Author Comment

by:esk
ID: 2578441
progressform: TProgressForm;

undeclared intentifier

Esk
0
 
LVL 17

Expert Comment

by:inthe
ID: 2579001
?  form1 : tform1  ...
0
 
LVL 1

Expert Comment

by:wmckie
ID: 2580540
What about using SHFileOperation, I've used this wrapper with Win95:

function FileOperation(FFrom, FTo: string; Funct, Flags: integer): boolean;

{ Funct values:

   FO_COPY    Copy the files specified by the pFrom member to the
              location specified by the pTo member.

   FO_DELETE  Delete the files specified by the pFrom member.
              The pTo member is ignored.

   FO_MOVE    Move the files specified by the pFrom member to the
              location specified by the pTo member.

   FO_RENAME  Rename the files specified by the pFrom member.
              The pTo member is ignored.

  Flags values:

   FOF_ALLOWUNDO         The specified file is deleted to the recycle bin.

   FOF_FILESONLY         The operation is performed only on files if a
                         wildcard filename is specified (i.e. *.pas).

   FOF_MULTIDESTFILES    The pTo member contains one destination file for
                         each source file instead of one directory to which
                         all source files are deposited.

   FOF_NOCONFIRMATION    The user is never asked for confirmation, and the
                         operation continues as if a response of
                         ‘yes to all’ was indicated.

   FOF_NOCONFIRMMKDIR    Automatically creates a new directory if one is
                         needed.

   FOF_NOERRORUI         There is not visual indication if an error occurs.

   FOF_RENAMEONCOLLISION The source file is automatically given a new name,
                         such as ‘Copy #1 of..’, in a move, copy, or rename
                         operation if a file in the target directory already
                         has the same name.

   FOF_SILENT            Does not display a progress dialog box.

   FOF_SIMPLEPROGRESS    Displays a progress dialog box, but does not show
                         filenames.

   FOF_WANTMAPPINGHANDLE The hNameMappings member receives a handle to a
                         filename mapping object.
}

var
  FileDetails: TSHFileOpStruct;
begin
  with FileDetails do
    begin
      Wnd    := Application.Handle;
      wFunc  := Funct;
      pFrom  := PChar(FFrom + #0);
      pTo    := PChar(FTo + #0);
      fFlags := Flags;
    end;
  Result := (SHFileOperation(FileDetails) = 0);
end;

I've also seen several shareware & freeware components that probably do something similar.

Walter McKie

0
 
LVL 17

Expert Comment

by:inthe
ID: 2581511
hi walter ,
guess you missed my earlier comment,
nice wrapper for it you've pasted here though ..
0
 

Author Comment

by:esk
ID: 2585556
Thanks Barry!

Esk
0
 
LVL 1

Expert Comment

by:wmckie
ID: 2586980
inthe

Yes I did miss your earlier comment, sorry I realy must read things properly, however if my FileOperation function is useful thats good.

Walter
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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