Posts tagged MoveFileEx

How to copy or move file with progress?

1


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. :D 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.

How to mark your file for deletion after next reboot?

0


You might have seen, while un-installing some applications, the uninstaller says – “Some files remains and will be removed after next reboot”. How they can mark a file to be deleted after next restart?


The answer is MoveFileEx(). The MoveFileEx() have a special flag MOVEFILE_DELAY_UNTIL_REBOOT. By specifying it, the file will be marked for deletion during the next reboot. See the sample code snippet.

void MarkFileForDeletion(CString csFileName )
{
    MoveFileEx( csFileName, // Source File.
                0, // Destination as null.
                MOVEFILE_DELAY_UNTIL_REBOOT );
}


Actually, the MoveFileEx(), places the filename pair in the registry at – HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations. The PendingFileRenameOperations is of type REG_MULTI_SZ which can hold multiple strings. So that it can hold multiple pairs of source-destination filenames.


The MoveFileEx() is only applicable for Windows NT family. For Windows 95/98/Me you’ve to modify the WININIT.INI file. In that file, you’ve to modify the [rename] section. Since an old 95 box is not available, i couldn’t try it. If you have, just try and let me know!


Targeted Audience – Intermediate.

Go to Top