Solved

API -> CopyFile - DeleteFile - RenameFile ( functions )

Posted on 2000-03-02
11
822 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Machine not responding during CopyFile() 3 89
select query - oracle 16 95
Performance of SQL statement 37 101
RESTRequest Parameter 4 15
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now