Solved

API -> CopyFile - DeleteFile - RenameFile ( functions )

Posted on 2000-03-02
11
814 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

12 Experts available now in Live!

Get 1:1 Help Now