Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6045271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:06:23+00:00 2026-05-23T07:06:23+00:00

I have found examples of CopyFileEx with progress, but I need to copy some

  • 0

I have found examples of CopyFileEx with progress, but I need to copy some files from a folder with overall progress.

Can anybody provide info how to do this? Or is there good alternative (component, function)?

Big thanks for help!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:06:23+00:00Added an answer on May 23, 2026 at 7:06 am

    Well, I had an answer – but I only just got around to digging it out 🙁 But here it is anyway, I wrote this a few years ago as part of a program that was called “CopyFilesAndFailGraceFully.exe” 🙂 I’ve modded it a bit to miss out the recovery stuff that handles failing hard drives if it can – so NOT FULLY TESTED but run as a simple test.

    You can call it to get a recursive filecount, filesize or Copy the files in a folder to a new folder. Or Mod for your own situation 🙂 Anyway its an example of what you need.

    unit FileCopierU;
    (***************************************************************
      Author Despatcher (Timbo) 2011
    ****************************************************************)
    interface
    
    uses
      Windows, Messages, SysUtils, Classes, controls, stdctrls, strUtils, ComCtrls, ShellApi, Math;
    
    Type
      TFolderOp = (foCopy, foCount, foSize);
      TCopyCallBack = function( TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred: int64;
                                StreamNumber, CallbackReason: Dword;
                                SourceFile, DestinationFile: THandle; Data: Pointer): DWord;
    
      TFileCopier = class(TPersistent)
      private
        fCopyCount: Integer;
        fFileCount: Integer;
        fFileSize: Int64;
        fCallBack: TCopyCallBack;
         function DoFolderFiles(const ASourcePath, ATargetPath: string; const Op: TFolderOp): Int64;
         function DoFolderTree(const ASourcePath, ATargetPath: string; const Op: TFolderOp): Int64;
      public
         constructor Create; virtual;
         function AddBackSlash(const S: String): string;
         function DoFiles(const ASourcePath, ATargetPath: string; const Op: TFolderOp): Int64;
         property CallBack: TCopyCallBack read fCallBack write fCallBack;
         property CopyCount: Integer read fCopyCount;
         property FileCount: Integer read fFileCount;
         property FileSize: Int64 read fFileSize;
      end;
    
    implementation
    
    { TFileCopier }
    
    function TFileCopier.AddBackSlash(const S: String): string;
    begin
      Result := S;
      if S <> '' then
      begin
        If S[length(S)] <> '\' then
          Result := S + '\';
      end
      else
        Result := '\';
    end;
    
    function TFileCopier.DoFiles(const ASourcePath, ATargetPath: string;
      const Op: TFolderOp): Int64;
    begin
      case Op of
       foCopy: fCopyCount := 0;
       foCount: fFileCount := 0;
       foSize: fFileSize:= 0;
      end;
      Result := DoFolderTree(ASourcePath, ATargetPath, Op);
    end;
    
    constructor TFileCopier.Create;
    begin
      inherited;
      CallBack := nil;
    end;
    
    function TFileCopier.DoFolderFiles( const ASourcePath, ATargetPath: string;
                                        const Op: TFolderOp): Int64;
    // Return -1: failed/error x: count of to or count of copied or Size of all files
    // Root paths must exist
    var
      StrName,
      MySearchPath,
      MyTargetPath,
      MySourcePath: string;
      FindRec: TSearchRec;
      i: Integer;
      Cancelled: Boolean;
      Attributes: WIN32_FILE_ATTRIBUTE_DATA;
    begin
      Result := 0;
      Cancelled := False;
      MyTargetPath := AddBackSlash(ATargetPath);
      MySourcePath := AddBackSlash(ASourcePath);
      MySearchPath := AddBackSlash(ASourcePath) + '*.*';
      i := FindFirst(MySearchPath, 0 , FindRec);
      try
        while (i = 0) and (Result <> -1) do
        begin
          try
          case op of
           foCopy: begin
              StrName := MySourcePath + FindRec.Name;
              if CopyFileEx(PWideChar(StrName), PWideChar(MyTargetPath + FindRec.Name), @fCallBack, nil, @Cancelled, COPY_FILE_FAIL_IF_EXISTS) then
              begin
                inc(Result);
                inc(fCopyCount);
              end
              else
                Result := -1;
            end;
           foCount:
           begin
             Inc(Result);
             Inc(fFileCount);
           end;
           foSize:
           begin
             Result := Result + FindRec.Size;
             fFileSize := fFileSize + FindRec.Size;
           end;
          end; // case
          except
            Result := -1;
          end;
          i := FindNext(FindRec);
        end;
      finally
        FindClose(FindRec);
      end;
    
    end;
    
    function TFileCopier.DoFolderTree( const ASourcePath, ATargetPath: string;
                                         const Op: TFolderOp): Int64;
    // Return -1: failed/error x: count of to or count of copied or Size of all files
    // Root paths must exist
    // Recursive
    var
      FindRec: TSearchRec;
      StrName, StrExt,
      MySearchPath,
      MyTargetPath,
      MySourcePath: string;
      InterimResult :Int64;
      i: Integer;
    begin
      Result := 0;
      // Find Folders
      MySearchPath := AddBackSlash(ASourcePath) + '*.*';
      MySourcePath := AddBackSlash(ASourcePath);
      MyTargetPath := AddBackSlash(ATargetPath);
      i := FindFirst(MySearchPath, faDirectory , FindRec);
      try
        while (i = 0) and (Result <> -1) do
        begin
          StrName := FindRec.Name;
          if (Bool(FindRec.Attr and faDirectory)) and (StrName <> '.') and (StrName <> '..') then
          begin
            try
              case op of
               foCopy:
                 if CreateDir(MyTargetPath + StrName) then
                  begin
                    InterimResult := DoFolderTree(MySourcePath + StrName, MyTargetPath + StrName, Op);
                    if InterimResult <> -1 then
                    begin
                      Result := Result + InterimResult;
                      fCopyCount := Result;
                    end
                    else
                      Result := -1;
                  end; // foCopy
               foCount, foSize:
               begin
                 InterimResult := DoFolderTree(MySourcePath + StrName, MyTargetPath + StrName, Op);
                 if InterimResult <> -1 then
                   Result := Result + InterimResult
                 else
                   Result := -1;  // or result, -1 easier to read
               end; // foCount, foSize
              end; // case
            except
              Result := -1;
            end;
          end;
          i := FindNext(FindRec);
        end;
      finally
        FindClose(FindRec);
      end;
      if Result <> -1 then
      case op of
       foCopy:
        begin
         InterimResult := DoFolderFiles( AddBackSlash(ASourcePath), AddBackSlash(ATargetPath), Op);
         if InterimResult <> -1 then
         begin
           Result := Result + InterimResult;
           fCopyCount := Result;
         end
         else
           Result := InterimResult;
        end;
       foCount:
       begin
         InterimResult := DoFolderFiles(AddBackSlash(ASourcePath), AddBackSlash(ATargetPath), Op);
         if InterimResult <> -1 then
         begin
           Result := Result + InterimResult;
           fFileCount := Result;
         end
         else
           Result := InterimResult;
       end; // foCount
       foSize:
       begin
         InterimResult := DoFolderFiles(AddBackSlash(ASourcePath), AddBackSlash(ATargetPath), Op);
         if InterimResult <> -1 then
         begin
           Result := Result + InterimResult;
           fFileSize := Result;
         end
         else
           Result := InterimResult;
       end; // foSize
      end; // case
    end;
    
    
    end.
    

    Its an Object (As you see) to use it (roughly):
    You will need a couple of vars appropriately named.
    Declare your callback:

      function CallBack(TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred: int64; StreamNumber, CallbackReason: Dword; SourceFile, DestinationFile: THandle; Data: Pointer): DWord;
    

    and implement:

    function CallBack( TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred: int64;
                              StreamNumber, CallbackReason: Dword;
                              SourceFile, DestinationFile: THandle;
                              Data: Pointer): DWord;
    begin
      if CopyStream <> StreamNumber then
      begin
        inc(CopyCount);
        CopyStream :=  StreamNumber;
      end;
      Result := PROGRESS_CONTINUE;
      Form1.lblCount.Caption := 'Copied' + IntToStr(CopyCount);
      application.ProcessMessages;
    end;
    

    Then call as needed 🙂 e.g.:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Copier: TFileCopier;
    begin
      Copier:= TFileCopier.Create;
      try
      Copier.CallBack := CallBack;
      CopyStream := 1;
      CopyCount := 0;
      Copier.DoFiles(MyCopyFolder, MyTargetFolder, foCount);
      Copier.DoFiles(MyCopyFolder, MyTargetFolder, foSize);
      Copier.DoFiles(MyCopyFolder, MyTargetFolder, foCopy);
      finally
        lblCount.Caption := 'Copied: ' + IntToStr(Copier.CopyCount) + ' Size: ' + IntToStr(Copier.FileSize) + ' Total: ' + IntToStr(Copier.FileCount);
        Copier.Free;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found some code that I need to use for my application but
I'm trying add vertical lines to my grid. I have found some examples but
I have found various examples of using jquery to wrap selected text from a
I have done some research, and majority of the examples I have found use
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
Firstly, I have found many examples of how to grab data from a db
I can use FQL multiquery in batch request? I have not found examples in
I have found other examples of people having this problem but have had no
i have found several examples/questions about getting row numbers of the selected cells but
I have found a few examples that relate to WPF, but none for Silverlight.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.