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
Solved

API -> CopyFile - DeleteFile - RenameFile ( functions )

Posted on 2000-03-02
11
831 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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 …

860 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