Solved

API -> CopyFile - DeleteFile - RenameFile ( functions )

Posted on 2000-03-02
11
826 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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.
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 …

809 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