Gold mine of Visual C++ tricks!
How to copy or move file with progress?
![]()
In old Win98 days, File copying progress notification was horrific. Even if you have a lot of data to copy, it always says – you have 5 more minutes to finish.
Well, how to copy or move file with accurate progress?

![]()
You can use the api – CopyFileEx() for copying data with progress and MoveFileEx() to move files with progress. Both are more or less similar in usage. In both cases, just call the apis by providing the callback function pointer. System will give callback notification for the progress. See the code snippet of CopyFileEx().
// Callback function for handling progress notification.
DWORD CALLBACK CopyProgressRoutine(
LARGE_INTEGER TotalFileSize,
LARGE_INTEGER TotalBytesTransferred,
LARGE_INTEGER StreamSize,
LARGE_INTEGER StreamBytesTransferred,
DWORD dwStreamNumber,
DWORD dwCallbackReason,
HANDLE hSourceFile,
HANDLE hDestinationFile,
LPVOID lpData )
{
// Calculate the percentage here.
double Percentage = ( double(TotalBytesTransferred.QuadPart) /
double(TotalFileSize.QuadPart) ) * 100;
// Continue the file copy. It can also be stopped.
return PROGRESS_CONTINUE;
}
void CDlgDlg::OnCopy()
{
// Copy the file.
CopyFileEx( _T("c:\\Jijo\\Games.rar"),
_T("c:\\Jijo\\GamesBackup.rar"),
CopyProgressRoutine,
0,
FALSE,
0 );
}
![]()
CopyFIleEx() and MoveFileEx() are available only from Windows NT onwards. So don’t forget to add _WIN32_WINNT=0×0400 to project settings.
![]()
Targeted Audience – Beginners.
| Print article | This entry was posted by Jijo Raj on August 28, 2008 at 6:06 pm, and is filed under Codeproject, Windows APIs. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |